繁体   English   中英

列表视图中的项目重复

[英]Item in listview is repeating

我有以下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;
    }

}

在我的自定义列表视图中,项目重复出现。所有项目的位置均相同。 错误在哪里? 如何避免此错误?

设置适配器的代码如下:

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

原因是Listview中的View在滚动时被回收。 像这样更改您的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;
        }

那是因为您对Viewholder的实现是错误的。 您没有将数据设置到回收的视图上。 您可以完全删除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;
}

另外,我建议使用RecyclerView而不是ListView。

膨胀是一个非常常见的问题,只需确保您应该清除适配器中的每个条件即可;即对于每个if条件,都应该包含有意义的else条件,因为每次滚动都将调用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();
        }
/////////////////////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