简体   繁体   English

自定义listView重复值并在滚动时更改它们

[英]Custom listView repeating values and changing them on scrolling

I have made a listview with a custom adapter. 我已经使用自定义适配器创建了一个listview。 There are five textviews, 1 checkbox and one button. 一共有五个textview,一个复选框和一个按钮。

-------------------------------------
    <TextView>  
    <TextView>   <checkbox>
    <TextView>  
    <TextView>    <button>
    <TextView>  
-------------------------------------

The arraylists that I am using to populate the textviews in the list has proper unique data. 我用来在列表中填充文本视图的arraylist具有正确的唯一数据。 But the listview takes only four to five values of the begining and repeates it in the rest of the list. 但是listview仅采用开始的4到5个值,并在列表的其余部分重复。

Also when I scroll the values displayed change automatically. 同样,当我滚动时,显示的值也会自动更改。 For example item A was displayed at first position. 例如,项目A显示在第一个位置。 So when I scroll down and then come up again, item C or item D is displayed. 因此,当我向下滚动然后再次出现时,将显示项目C或项目D。

It is really confusing me! 这真让我感到困惑! Please help! 请帮忙!

Custom Adapter code 自定义适配器代码

protected class MyCustomAdapter extends ArrayAdapter<String>
{
    int len;
    private SparseBooleanArray mCheckStates;
    private SparseBooleanArray favourites;

    ArrayList<String> myMake, myModel, myVer, myPrice, myPlace, sellr_pos;
    int count = 0;
    ViewHolder holder = null;
    Boolean status;

    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<String> car_make, ArrayList<String> car_model,
            ArrayList<String> car_version, ArrayList<String> car_price,
            ArrayList<String> car_place, ArrayList<String> sellr_pos) {
        super(context, textViewResourceId);

        mCheckStates = new SparseBooleanArray(car_model.size());
        favourites = new SparseBooleanArray(car_model.size());

        myMake = car_make;
        myModel = car_model;
        myVer = car_version;
        myPrice = car_price;
        myPlace = car_place;

    }

    private class ViewHolder {
        TextView txt1, txt2, txt3, txt4, txt5;
        CheckBox chkbox;
        Button btn_fav;
    }

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

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

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.text_adaptr, null);
            holder = new ViewHolder();

            holder.txt1 = (TextView) convertView.findViewById(R.id.text1);
            holder.txt2 = (TextView) convertView.findViewById(R.id.text5);
            holder.txt3 = (TextView) convertView.findViewById(R.id.text3);
            holder.txt4 = (TextView) convertView.findViewById(R.id.text4);
            holder.txt5 = (TextView) convertView.findViewById(R.id.text2);

            System.out.println("ListView position: " + position);

            holder.txt1.setText(myMake.get(position));
            holder.txt2.setText(myModel.get(position));
            holder.txt3.setText(myVer.get(position));
            holder.txt4.setText(myPrice.get(position));
            holder.txt5.setText(myPlace.get(position));

            holder.chkbox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            holder.btn_fav = (Button) convertView.findViewById(R.id.fav);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.chkbox.setTag(position);

        holder.chkbox.setChecked(mCheckStates.get(position, false));

        if (favourites.get(position, false)) {

            holder.btn_fav.setBackgroundResource(R.drawable.star);


        } else {
            holder.btn_fav.setBackgroundResource(R.drawable.star_grey);

        }

        len = fav_ids.size();

        System.out.println("FAV_IDS len:" + len);

        for (int t = 0; t < len; t++) {

            String pos_id = pos.get(position).trim();
            String fav_id = fav_ids.get(t).trim();

            if (pos_id.equals(fav_id)) {


                holder.btn_fav.setBackgroundResource(R.drawable.star);

                break;

            } else {

                holder.btn_fav.setBackgroundResource(R.drawable.star_grey);
            }

        }

        holder.chkbox.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            // Do something

            }
        });

        holder.btn_fav.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // Do something

            }
        });

        return convertView;
    }
}

Set Values to the TextView at 将值设置为TextView

if (convertView == null) {
....code...
........../.

/*
you are setting values here
remove it from here
*/
 holder.txt1.setText(myMake.get(position));
            holder.txt2.setText(myModel.get(position));
            holder.txt3.setText(myVer.get(position));
            holder.txt4.setText(myPrice.get(position));
            holder.txt5.setText(myPlace.get(position));
}
else
{
....code...
}


/*
Set values here
it will solve the problem
*/
holder.txt1.setText(myMake.get(position));
            holder.txt2.setText(myModel.get(position));
            holder.txt3.setText(myVer.get(position));
            holder.txt4.setText(myPrice.get(position));
            holder.txt5.setText(myPlace.get(position));

It solves my problem 它解决了我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM