重复Android Listview项目

时间:2016-11-01 05:53:44

标签: android listview repeat

首次加载我的列表视图时,它显示正确的项目,当我返回并再次加载时,它将显示重复的所有项目。再次,当我返回并加载相同的条目再次重复。

例如:第一次有4件,第二次有8件,第三件是12件,依此类推。

以下是我的customarrayadapter's getView()方法。

public class CustomArrayAdapterHistory extends ArrayAdapter {

ArrayList<RequestHistory> historyList = new ArrayList<>();
private Context context;

public CustomArrayAdapterHistory(Context context, int textViewResourceId, ArrayList<RequestHistory> objects) {
    super(context, textViewResourceId, objects);
    historyList = objects;
    this.context=context;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.request_history_items, null);
    TextView textView1 = (TextView) v.findViewById(R.id.textView1);
    TextView textView2 = (TextView) v.findViewById(R.id.textView2);
    TextView textView3 = (TextView) v.findViewById(R.id.textView3);
    ImageView imageView= (ImageView) v.findViewById(R.id.imageView);
    TextView date= (TextView) v.findViewById(R.id.date);

    Log.e("Date","....."+historyList.get(position).getDate());
    date.setText(historyList.get(position).getDate());
    if(historyList.get(position).getserviceName()!="null")
        if(historyList.get(position).getserviceName()!="")
            textView1.setText(historyList.get(position).getserviceName());
    if(historyList.get(position).getreplyNote().equals("")){
        textView3.setVisibility(View.INVISIBLE);
    }
    else {
        textView3.setText("Reply: " + historyList.get(position).getreplyNote());
    }
    textView2.setText("Booking Details: " + historyList.get(position).getBookingdetails());


    if(historyList.get(position).getStatus().equals("0")){
       imageView.setBackgroundResource(R.drawable.pending);
    }
    else if(historyList.get(position).getStatus().equals("1")){
       Log.e("STATUS ID","....."+historyList.get(position).getStatus());
       imageView.setBackgroundResource(R.drawable.cross);
    }
    else if(historyList.get(position).getStatus().equals("2")){
       imageView.setBackgroundResource(R.drawable.tick);
    }
    return v;

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

你的getView方法项应该是这样的

if( convertView == null ){
        //We must create a View:
        convertView = inflater.inflate(R.layout.my_list_item, parent, false);
    }
    //Here we can do changes to the convertView, such as set a text on a TextView 
    //or an image on an ImageView.
    return convertView;

答案 1 :(得分:0)

检查适配器构造函数中的第三个参数ArrayList<RequestHistory> objects。每次调用构造函数时,它都会扩展它。

答案 2 :(得分:0)

getCount更改为以下后,您的适配器没问题。在使用适配器的活动中,您必须清除并重新分配传递给适配器构造函数的列表,然后再调用它。

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