[英]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.