简体   繁体   中英

Item in listview is repeating

I have the following 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. Change your getView() like this:

    @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. You are not setting the data onto the recycled views. You can completely remove the else block and just set your data.

 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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