ListView复制数据

时间:2018-05-03 11:52:44

标签: java android listview arraylist

在其中一个网页上安装一个ListView的Android应用程序。 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();
        }
    });
}

上述方法将所有相关数据添加到数组列表中,并将其添加到适配器中。

自定义适配器

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();
}
}

My Bids 1

My Bids 2

When you click the bottom Panasonic TV

如果需要,可以提供更多代码。

修改

代码按预期运行,直到它到达数组中的第4个元素,一旦该点被命中,它就会进入我认为适用于适配器的基类java类。就好像它忽略了最后两个并回收了观点。

1 个答案:

答案 0 :(得分:0)

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

我认为问题出在上面的适配器方法中。将此更改为:

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