简体   繁体   English

列表视图中的项目重复

[英]Item in listview is repeating

I have the following arrayadapter . 我有以下arrayadapter。

public class SmsArrayAdapter extends ArrayAdapter<String> {

    List<String> smsBody;
    List<Boolean> Status;
    List<String> time;
    List<String> SmsMessageId;

    Context context;
    private static LayoutInflater inflater = null;
    String fromNumber;

    public SmsArrayAdapter(Context context, int resource, List<String> smsBody,
            List<Boolean> Status, List<String> time, List<String> SmsMessageId,
            String fromNumber) {
        super(context, resource, smsBody);
        this.smsBody = smsBody;
        this.Status = Status;
        this.context = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.fromNumber = fromNumber;
        this.time = time;
        this.SmsMessageId=SmsMessageId;
    }

    public String getStr(int position) {
        return smsBody.get(position);
    }
    public String getId(int position)
    {
        return SmsMessageId.get(position);
    }
    public void setRead(int position,String smsMessageId)
    {
        Status.set(position, true);
        ContentValues values = new ContentValues();
        values.put("read", true);
        context.getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=" +smsMessageId, null);
    }
    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return smsBody.get(position);
    }

    public static class ViewHolder {
        public TextView textfrom;
        public TextView text_sms;
        public TextView text_time;
    }

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

        ViewHolder holder;
        if (convertView == null) {

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            convertView = inflater.inflate(R.layout.row_item, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();

            holder.textfrom = (TextView) convertView
                    .findViewById(R.id.textView_from);
            holder.text_sms = (TextView) convertView
                    .findViewById(R.id.textView_sms);
            holder.text_time = (TextView) convertView
                    .findViewById(R.id.textView_time);

            holder.textfrom.setText(" " + fromNumber);

            String smsTextToDisplay = smsBody.get(position);
            if (smsTextToDisplay.length() > 100)
                smsTextToDisplay = smsTextToDisplay.substring(0, 99) + " ...";

            holder.text_sms.setText(smsTextToDisplay);


            holder.text_time.setText(time.get(position));
            if (Status.get(position) == false) {
                convertView.setBackgroundColor(context.getResources().getColor(
                        R.color.light_blue_overlay));

            }

            /************ Set holder with LayoutInflater ************/
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();



        return convertView;
    }

}

In my customized list view items are repeating.position of item is same for all item. 在我的自定义列表视图中,项目重复出现。所有项目的位置均相同。 Where is the error ? 错误在哪里? How can I avoid this error ? 如何避免此错误?

The code of set adapter is as follows : 设置适配器的代码如下:

arrayAdapter = new SmsArrayAdapter(this, R.layout.row_item, smsBody,
                status, time, SmsMessageId, fromNumber);
        smsListView.setAdapter(arrayAdapter);
        smsListView.setOnItemClickListener(this);

The reason is Views in Listview are recycled upon scroll. 原因是Listview中的View在滚动时被回收。 Change your getView() like this: 像这样更改您的getView()

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

            ViewHolder holder;
            if (convertView == null) {

                /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
                convertView = inflater.inflate(R.layout.row_item, null);

                /****** View Holder Object to contain tabitem.xml file elements ******/

                holder = new ViewHolder();

                holder.textfrom = (TextView) convertView
                        .findViewById(R.id.textView_from);
                holder.text_sms = (TextView) convertView
                        .findViewById(R.id.textView_sms);
                holder.text_time = (TextView) convertView
                        .findViewById(R.id.textView_time);

/************ Set holder with LayoutInflater ************/
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

                holder.textfrom.setText(" " + fromNumber);

                String smsTextToDisplay = smsBody.get(position);
                if (smsTextToDisplay.length() > 100)
                    smsTextToDisplay = smsTextToDisplay.substring(0, 99) + " ...";

                holder.text_sms.setText(smsTextToDisplay);


                holder.text_time.setText(time.get(position));
                if (Status.get(position) == false) {
                    convertView.setBackgroundColor(context.getResources().getColor(
                            R.color.light_blue_overlay));

                } 

            return convertView;
        }

That is because your implementation of the Viewholder is wrong. 那是因为您对Viewholder的实现是错误的。 You are not setting the data onto the recycled views. 您没有将数据设置到回收的视图上。 You can completely remove the else block and just set your data. 您可以完全删除else块,而只需设置数据。

 ViewHolder holder;
    if (convertView == null) {

        /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
        convertView = inflater.inflate(R.layout.row_item, null);

        /****** View Holder Object to contain tabitem.xml file elements ******/

        holder = new ViewHolder();

        holder.textfrom = (TextView) convertView
                .findViewById(R.id.textView_from);
        holder.text_sms = (TextView) convertView
                .findViewById(R.id.textView_sms);
        holder.text_time = (TextView) convertView
                .findViewById(R.id.textView_time);

        }

        /************ Set holder with LayoutInflater ************/
        convertView.setTag(holder);
    }

    holder = (ViewHolder) convertView.getTag();

    holder.textfrom.setText(" " + fromNumber);
    String smsTextToDisplay = smsBody.get(position);
    if (smsTextToDisplay.length() > 100)
        smsTextToDisplay = smsTextToDisplay.substring(0, 99) + " ...";

    holder.text_sms.setText(smsTextToDisplay);


    holder.text_time.setText(time.get(position));
    if (Status.get(position) == false) {
        convertView.setBackgroundColor(context.getResources().getColor(
                R.color.light_blue_overlay));

    return convertView;
}

In addition I'd recommend using RecyclerView instead of ListView. 另外,我建议使用RecyclerView而不是ListView。

It is very Common Problem in inflating, Just make sure that you should clear every condition in your adapter ie for every if condition there should be meaningful else condition because your getView method will be called upon every scrolling to recycle views. 膨胀是一个非常常见的问题,只需确保您应该清除适配器中的每个条件即可;即对于每个if条件,都应该包含有意义的else条件,因为每次滚动都将调用getView方法以回收视图。

This is the approach I have implemented when I got same problem, Please ignore if this wouldn't help you. 这是我遇到相同问题时所采用的方法,请忽略这是否有帮助。

Thanks 谢谢

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

        ViewHolder holder;
        if (convertView == null) {

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            convertView = inflater.inflate(R.layout.row_item, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();

            holder.textfrom = (TextView) convertView
                    .findViewById(R.id.textView_from);
            holder.text_sms = (TextView) convertView
                    .findViewById(R.id.textView_sms);
            holder.text_time = (TextView) convertView
                    .findViewById(R.id.textView_time);

            /************ Set holder with LayoutInflater ************/
            convertView.setTag(holder);
        } else{
            holder = (ViewHolder) convertView.getTag();
        }
/////////////////////Added///////////////
            holder.textfrom.setText(" " + fromNumber);

            String smsTextToDisplay = smsBody.get(position);
            if (smsTextToDisplay.length() > 100)
                smsTextToDisplay = smsTextToDisplay.substring(0, 99) + " ...";

            holder.text_sms.setText(smsTextToDisplay);


            holder.text_time.setText(time.get(position));
            if (Status.get(position) == false) {
                convertView.setBackgroundColor(context.getResources().getColor(
                        R.color.light_blue_overlay));

            }
////////Added//////////////

        return convertView;
    }

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

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