Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ListView复制数据_Java_Android_Listview_Arraylist - Fatal编程技术网

Java ListView复制数据

Java ListView复制数据,java,android,listview,arraylist,Java,Android,Listview,Arraylist,有一个android应用程序,在其中一个页面上有一个ListView。ListView有一个数组,其中包含5个对象,该数组被传递到列表适配器中以显示。在列表视图中,它显示了5个项目,但底部的一个项目显示为重复项。但是,当您单击此项时,它将显示正确的数据,这些数据不会在其他页面上重复。下面是代码和屏幕截图 MyJobsFragment.java private void bidOnList() throws ParseException { final ArrayList<Strin

有一个android应用程序,在其中一个页面上有一个
ListView
ListView
有一个数组,其中包含5个对象,该数组被传递到列表适配器中以显示。在列表视图中,它显示了5个项目,但底部的一个项目显示为重复项。但是,当您单击此项时,它将显示正确的数据,这些数据不会在其他页面上重复。下面是代码和屏幕截图

MyJobsFragment.java

private void bidOnList() throws ParseException
{
    final ArrayList<String> jobsListArray = new ArrayList<>();

    // Iterate through entire bids table
    for (DataSnapshot ds : getBidListChildren())
    {
        // Iterate through the actual bids information
        Iterable<DataSnapshot> bidsSnapShot = ds.getChildren();

        for (DataSnapshot ds1 : bidsSnapShot)
        {
            // if the User Id equals the current user added to a list
            if (getBidInformation(ds1).getUserID().equals(auth.getCurrentUser().getUid()))
            {
                boolean active = ds1.child("active").getValue(boolean.class);
                if (active)
                {
                    jobsListArray.add(ds.getKey());
                }
            }
        }
    }

    // Go through the jobs table
    for (DataSnapshot ds3 : getJobListChildren())
    {
        /*
           If the job is in the jobsListArray previously and the status is Pending
            Add to the jobsList
        */
        if (jobsListArray.contains(ds3.getKey()) && getJobInformation(ds3).getJobStatus().equals("Pending"))
        {
            Date sdf = new SimpleDateFormat("dd/MM/yyyy").parse(genericMethods.getJobInformation(ds3).getCollectionDate());

            if (new Date().before(sdf))
            {
                jobListKey.add(ds3.getKey());
                jobList.add(getJobInformation(ds3));
            }
        }
    }

    // Display information in ListView
    mAdapterBidOn = new MyCustomAdapterForTabViews(getActivity(), isAdded(), host, getLayoutInflater(), getFragmentManager());
    mAdapterBidOn.addKeyArray(jobListKey);
    mAdapterBidOn.addArray(jobList);

    jobListViewBidOn.setAdapter(mAdapterBidOn);

    // Press on the object and go view all the Job Information and Bids
    jobListViewBidOn.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            BidOnJobsFragment bidOnJobsFragment = new BidOnJobsFragment();
            Bundle bundle = new Bundle();
            bundle.putSerializable("Job", mAdapterBidOn.mData.get(position));
            bundle.putSerializable("JobId", mAdapterBidOn.mDataKeys.get(position));
            bidOnJobsFragment.setArguments(bundle);
            getFragmentManager().beginTransaction().replace(R.id.content, bidOnJobsFragment).addToBackStack("tag").commit();
        }
    });
}
private void bidOnList()引发异常
{
最终ArrayList作业Starray=新ArrayList();
//遍历整个bids表
对于(DataSnapshot ds:getBidListChildren())
{
//反复浏览实际的投标信息
Iterable bidsSnapShot=ds.getChildren();
对于(DataSnapshot ds1:bidsSnapShot)
{
//如果用户Id等于添加到列表中的当前用户
if(getBidInformation(ds1).getUserID()等于(auth.getCurrentUser().getUid())
{
boolean-active=ds1.child(“active”).getValue(boolean.class);
如果(活动)
{
jobslistaray.add(ds.getKey());
}
}
}
}
//检查一下工作表
对于(DataSnapshot ds3:getJobListChildren())
{
/*
如果作业位于作业列表中,且状态为“挂起”
添加到作业列表中
*/
if(jobslistaray.contains(ds3.getKey())&&getJobInformation(ds3.getJobStatus().equals(“待定”))
{
Date sdf=new SimpleDateFormat(“dd/MM/yyyy”).parse(genericMethods.getJobInformation(ds3.getCollectionDate());
如果(新日期()之前(sdf))
{
jobListKey.add(ds3.getKey());
jobList.add(getJobInformation(ds3));
}
}
}
//在ListView中显示信息
mAdapterBidOn=新的MyCustomAdapterPortabViews(getActivity(),isAdded(),host,getLayoutInflater(),getFragmentManager());
addKeyArray(jobListKey);
addArray(作业列表);
setAdapter(mAdapterBidOn);
//按该对象,然后查看所有作业信息和出价
jobListViewBidOn.setOnItemClickListener(新的AdapterView.OnItemClickListener()
{
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id)
{
BidOnJobsFragment BidOnJobsFragment=新的BidOnJobsFragment();
Bundle=新Bundle();
bundle.putSerializable(“Job”,mAdapterBidOn.mData.get(position));
bundle.putSerializable(“JobId”,mAdapterBidOn.mDataKeys.get(position));
bidOnJobsFragment.setArguments(bundle);
getFragmentManager();
}
});
}
上述方法将所有相关数据添加到数组列表中,并将其添加到适配器中

自定义适配器

public class MyCustomAdapterForTabViews extends BaseAdapter
{

public ArrayList<JobInformation> mData = new ArrayList<>();
public ArrayList<JobInformation> mDataOrig = new ArrayList<>();
public ArrayList<String> mDataKeys = new ArrayList<>();
private TabHost host;

public LayoutInflater mInflater;
private FragmentActivity fragmentActivity;
private LayoutInflater layoutInflater;
private FragmentManager fragmentManager;


public MyCustomAdapterForTabViews(FragmentActivity fragmentActivity, boolean isAdded, TabHost host, LayoutInflater layoutInflater, FragmentManager fragmentManager)
{
    if (isAdded)
    {
        this.fragmentActivity = fragmentActivity;
        this.fragmentManager = fragmentManager;
        this.layoutInflater = layoutInflater;
        mInflater = (LayoutInflater) fragmentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    this.host = host;
}

public void addItem(final JobInformation item)
{
    mData.add(item);
    mDataOrig.add(item);
}


public void addArray(final ArrayList<JobInformation> j)
{
    mData.clear();
    mDataOrig.clear();
    mData = j;
    mDataOrig = j;
}

public void addKeyArray(final ArrayList<String> k)
{
    mDataKeys.clear();
    mDataKeys = k;
}

@Override
public int getCount()
{
    return mData.size();
}

@Override
public Object getItem(int position)
{
    return mData.get(position);
}

@Override
public long getItemId(int position)
{
    return 0;
}

@Override
public boolean hasStableIds()
{
    return false;
}

@Override
public void registerDataSetObserver(DataSetObserver observer)
{

}

@Override
public void unregisterDataSetObserver(DataSetObserver observer)
{

}

@Override
public boolean areAllItemsEnabled()
{
    return false;
}

@Override
public boolean isEmpty()
{
    return false;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    // Bid on holder
    MyCustomAdapterForTabViews.GroupViewHolderBidOn holderBidOn;
    // Accepted holder
    final MyCustomAdapterForTabViews.GroupViewHolderAccepted holderAccepted;
    // Completed holder
    MyCustomAdapterForTabViews.GroupViewHolderCompleted holderCompleted;

    if (convertView == null)
    {
        // Bid on
        if (host.getCurrentTab() == 0)
        {
            convertView = mInflater.inflate(R.layout.job_info_bid_on, null);
            holderBidOn = new MyCustomAdapterForTabViews.GroupViewHolderBidOn();

            holderBidOn.textViewJobName = convertView.findViewById(R.id.textName);
            holderBidOn.imageViewCross = convertView.findViewById(R.id.imageViewCross);
            holderBidOn.imageViewEditPen = convertView.findViewById(R.id.imageViewEditPen);
            holderBidOn.textViewJobDescription = convertView.findViewById(R.id.textDesc);
            holderBidOn.textViewAddressFrom = convertView.findViewById(R.id.textAddressFrom);
            holderBidOn.textViewAddressTo = convertView.findViewById(R.id.textAddressTo);

            holderBidOn.textViewJobName.setText(mData.get(position).getAdvertName());
            holderBidOn.textViewJobDescription.setText(mData.get(position).getAdvertDescription());
            holderBidOn.textViewAddressFrom.setText(mData.get(position).getColL1() + ", " + mData.get(position).getColTown() + ", " + mData.get(position).getColPostcode());
            holderBidOn.textViewAddressTo.setText(mData.get(position).getDelL1() + ", " + mData.get(position).getDelPostcode() + ", " + mData.get(position).getDelPostcode());

            holderBidOn.imageViewCross.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    final DatabaseConnections databaseConnections = new DatabaseConnections();

                    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(fragmentActivity);
                    View mView = layoutInflater.inflate(R.layout.popup_creator, null);

                    alertDialog.setTitle("Delete Job");
                    alertDialog.setView(mView);
                    final AlertDialog dialog = alertDialog.create();
                    dialog.show();

                    TextView customText = mView.findViewById(R.id.textViewCustomText);
                    customText.setText("Are You Sure You Want To Delete " + mData.get(position).getAdvertName() + "?");

                    Button yesButton = mView.findViewById(R.id.yesButton);
                    Button noButton = mView.findViewById(R.id.noButton);

                    yesButton.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View view)
                        {
                            // My Adverts
                            if (mData.get(position).getPosterID().equals(databaseConnections.getCurrentUser()))
                            {
                                databaseConnections.getDatabaseReference().child("Jobs").child(mDataKeys.get(position)).addListenerForSingleValueEvent(new ValueEventListener()
                                {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot)
                                    {
                                        databaseConnections.getDatabaseReference().child("Jobs").child(mDataKeys.get(position))
                                                .child("jobStatus").setValue("Inactive");
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError)
                                    {

                                    }
                                });

                                databaseConnections.getDatabaseReference().child("Bids").child(mDataKeys.get(position)).addListenerForSingleValueEvent(new ValueEventListener()
                                {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot)
                                    {
                                        databaseConnections.getDatabaseReference().child("Bids").child(mDataKeys.get(position)).child(mData.get(position)
                                                .getCourierID()).child("active").setValue(false);
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError)
                                    {

                                    }
                                });
                            }
                            // My Jobs
                            else
                            {
                                databaseConnections.getDatabaseReference().child("Bids").child(mDataKeys.get(position)).addListenerForSingleValueEvent(new ValueEventListener()
                                {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot)
                                    {
                                        databaseConnections.getDatabaseReference().child("Bids").child(mDataKeys.get(position)).child(databaseConnections.getCurrentUser()).child("active").setValue(false);
                                        mData.remove(position);
                                        mDataKeys.remove(position);
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError)
                                    {

                                    }
                                });
                            }

                            notifyDataSetChanged();
                            dialog.dismiss();
                        }
                    });

                    noButton.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View view)
                        {
                            dialog.dismiss();
                        }
                    });
                }
            });

            DatabaseConnections databaseConnections = new DatabaseConnections();

            // My Adverts
            if (mData.get(position).getPosterID().equals(databaseConnections.getCurrentUser()))
            {
                holderBidOn.imageViewEditPen.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        GenericMethods genericMethods = new GenericMethods();
                        PostAnAdvertFragment postAnAdvertFragment = new PostAnAdvertFragment();

                        Bundle bundle = new Bundle();
                        bundle.putSerializable("JobInfo", mData.get(position));
                        bundle.putSerializable("JobIdKey", mDataKeys.get(position));
                        postAnAdvertFragment.setArguments(bundle);

                        genericMethods.beginTransactionToFragment(fragmentManager, postAnAdvertFragment);
                    }
                });
            }
            // My Jobs
            else
            {
                holderBidOn.imageViewEditPen.setVisibility(View.GONE);
            }

            convertView.setTag(holderBidOn);
        }
        // Accepted
        else if (host.getCurrentTab() == 1)
        {
            convertView = mInflater.inflate(R.layout.job_info_accepted, null);
            holderAccepted = new MyCustomAdapterForTabViews.GroupViewHolderAccepted();

            holderAccepted.textViewJobName = convertView.findViewById(R.id.textName);
            holderAccepted.textViewDescription = convertView.findViewById(R.id.textDesc);
            holderAccepted.textViewAddressFrom = convertView.findViewById(R.id.textAddressFrom);
            holderAccepted.textViewAddressTo = convertView.findViewById(R.id.textAddressTo);
            holderAccepted.textViewBid = convertView.findViewById(R.id.textBid);

            holderAccepted.textViewJobName.setText(mData.get(position).getAdvertName());
            holderAccepted.textViewDescription.setText(mData.get(position).getAdvertDescription());
            holderAccepted.textViewAddressFrom.setText(mData.get(position).getColL1() + ", " + mData.get(position).getColTown() + ", " + mData.get(position).getColPostcode());
            holderAccepted.textViewAddressTo.setText(mData.get(position).getDelL1() + ", " + mData.get(position).getDelPostcode() + ", " + mData.get(position).getDelPostcode());

            DatabaseConnections databaseConnections = new DatabaseConnections();
            databaseConnections.getDatabaseReference().child("Bids").child(mDataKeys.get(position)).child(mData.get(position).getCourierID()).addValueEventListener(new ValueEventListener()
            {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot)
                {
                    String acceptedBid = dataSnapshot.child("userBid").getValue(String.class);
                    holderAccepted.textViewBid.setText("£" + acceptedBid);
                }

                @Override
                public void onCancelled(DatabaseError databaseError)
                {

                }
            });

            convertView.setTag(holderAccepted);
        }
        // Completed
        else if (host.getCurrentTab() == 2)
        {
            convertView = mInflater.inflate(R.layout.job_info_list_completed, null);

            holderCompleted = new MyCustomAdapterForTabViews.GroupViewHolderCompleted();

            holderCompleted.textViewJobName = convertView.findViewById(R.id.textName);
            holderCompleted.imageViewCross = convertView.findViewById(R.id.imageViewCross);

            holderCompleted.textViewJobName.setText(mData.get(position).getAdvertName());
            holderCompleted.imageViewCross.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    final DatabaseConnections databaseConnections = new DatabaseConnections();

                    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(fragmentActivity);
                    View mView = layoutInflater.inflate(R.layout.popup_creator, null);

                    alertDialog.setTitle("Delete Job");
                    alertDialog.setView(mView);
                    final AlertDialog dialog = alertDialog.create();
                    dialog.show();

                    TextView customText = mView.findViewById(R.id.textViewCustomText);
                    customText.setText("Are You Sure You Want To Delete " + mData.get(position).getAdvertName() + "?");

                    Button yesButton = mView.findViewById(R.id.yesButton);
                    Button noButton = mView.findViewById(R.id.noButton);

                    yesButton.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View view)
                        {
                            // My Adverts
                            if (mData.get(position).getPosterID().equals(databaseConnections.getCurrentUser()))
                            {
                                databaseConnections.getDatabaseReference().child("Jobs").child(mDataKeys.get(position)).addListenerForSingleValueEvent(new ValueEventListener()
                                {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot)
                                    {
                                        databaseConnections.getDatabaseReference().child("Jobs").child(mDataKeys.get(position))
                                                .child("jobStatus").setValue("Inactive");
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError)
                                    {

                                    }
                                });

                            }
                            // My Jobs
                            else
                            {
                                databaseConnections.getDatabaseReference().child("Bids").child(mDataKeys.get(position)).addListenerForSingleValueEvent(new ValueEventListener()
                                {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot)
                                    {
                                        databaseConnections.getDatabaseReference().child("Bids").child(mDataKeys.get(position)).child(databaseConnections.getCurrentUser()).child("active").setValue(false);
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError)
                                    {

                                    }
                                });
                            }
                            notifyDataSetChanged();
                            dialog.dismiss();
                        }
                    });

                    noButton.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View view)
                        {
                            dialog.dismiss();
                        }
                    });
                }
            });

            convertView.setTag(holderCompleted);
        }
    } else
    {
        if (host.getCurrentTab() == 0)
        {
            holderBidOn = (MyCustomAdapterForTabViews.GroupViewHolderBidOn) convertView.getTag();
        } else if (host.getCurrentTab() == 1)
        {
            holderAccepted = (MyCustomAdapterForTabViews.GroupViewHolderAccepted) convertView.getTag();
        } else if (host.getCurrentTab() == 2)
        {
            holderCompleted = (MyCustomAdapterForTabViews.GroupViewHolderCompleted) convertView.getTag();
        }
    }

    return convertView;
}

public class GroupViewHolderBidOn
{
    public TextView textViewJobName;
    public TextView textViewJobDescription;
    public TextView textViewAddressFrom;
    public TextView textViewAddressTo;
    public ImageView imageViewCross;
    public ImageView imageViewEditPen;
}

public class GroupViewHolderAccepted
{
    public TextView textViewJobName;
    public TextView textViewDescription;
    public TextView textViewAddressFrom;
    public TextView textViewAddressTo;
    public TextView textViewBid;
}

public class GroupViewHolderCompleted
{
    public TextView textViewJobName;
    public ImageView imageViewCross;
}

public void filter(String charText)
{
    ArrayList<JobInformation> jobs = new ArrayList<>();
    ArrayList<JobInformation> jA = new ArrayList<>();
    charText = charText.toLowerCase(Locale.getDefault());

    if (charText.length() == 0)
    {
        mData = mDataOrig;
    } else
    {
        for (JobInformation j : mDataOrig)
        {
            if (j.getWholeString().toLowerCase(Locale.getDefault()).contains(charText))
            {
                jobs.add(j);
                jA.add(j);
            } else
            {
                jA.add(j);
            }
        }
        mData.clear();
        mData = jobs;
        mDataOrig = jA;
    }

    notifyDataSetChanged();
}
}
公共类MyCustomAdapterPortabViews扩展了BaseAdapter
{
public ArrayList mData=new ArrayList();
public ArrayList mDataOrig=new ArrayList();
public ArrayList mDataKeys=new ArrayList();
私有TabHost主机;
公共布局更扁平,更扁平;
私人碎片活动碎片活动;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
私人碎片管理器碎片管理器;
公共MyCustomAdapterForTabView(FragmentActivity FragmentActivity,添加布尔值,TabHost host,LayoutFlater LayoutFlater,FragmentManager FragmentManager)
{
如果(已添加)
{
this.fragmentActivity=fragmentActivity;
this.fragmentManager=fragmentManager;
this.layoutInflater=layoutInflater;
mInflater=(LayoutInflater)fragmentActivity.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
}
this.host=host;
}
公共作废附加项(最终工单信息项)
{
mData.add(项目);
mDataOrig.添加(项目);
}
public void addArray(最终数组列表j)
{
mData.clear();
mDataOrig.clear();
mData=j;
mDataOrig=j;
}
公共无效addKeyArray(最终ArrayList k)
{
mDataKeys.clear();
mDataKeys=k;
}
@凌驾
public int getCount()
{
返回mData.size();
}
@凌驾
公共对象getItem(int位置)
{
返回mData.get(位置);
}
@凌驾
公共长getItemId(int位置)
{
返回0;
}
@凌驾
公共布尔表ID()
{
返回false;
}
@凌驾
公共无效注册表DataSetObserver(DataSetObserver observer)
{
}
@凌驾
public void unregisteredDataSetobserver(DataSetObserver观察员)
{
}
@凌驾
公共布尔值areAllItemsEnabled()
{
返回false;
}
@凌驾
公共布尔值为空()
{
返回false;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图)
{
//持票人竞价
MyCustomAdapterPortabViews.GroupViewHolderBidOn holderBidOn;
//承兑持有人
最终MyCustomAdapterForTabViews.GroupViewHolderAccepted holderAccepted;
//完整持有人
MyCustomAdapterPortabViews.GroupViewHolderCompleted holderCompleted;
if(convertView==null)
{
//投标
if(host.getCurrentTab()==0)
{
convertView=mInflater.充气(R.layout.job\u info\u bid\u on,空);
holderBidOn=新的MyCustomAdapterPortabViews.GroupViewHolderBidOn();
holderBidOn.textViewJobName=convertView.findViewById(R.id.textName);
holderBidOn.imageViewCross=convertView.findViewById(R.id.imageViewCross);
holderBidOn.imageViewEditPen=convertView.findViewById(R.id.imageViewEditPen);
holderBidOn.textViewJobDescription=convertView.findViewById(R.id.textDesc);
holderBidOn.textViewAddressFrom=convertView.findViewById(R.id.textAddressFrom);
holderBidOn.textViewAddressTo=convertView.findViewById(R.id.textAddressTo);
holderBidOn.textViewJobN
@Override
public long getItemId(int position)
{
    return 0;
}
@Override
public long getItemId(int position)
{
    return position;
}