我在以下链接中使用了我的应用程序中的代码:
http://www.tutorialsbuzz.com/2014/03/watsapp-custom-listview-imageview-textview-baseadapter.html
但是当我使用CustomAdapter时,将ListView设置为此适配器, ListView不显示Member_name数组中的所有对象。
它的作用是,只显示该数组中的前9个对象,然后它从头开始重复显示这9个对象,反复显示,与数组大小一样多。
你能帮帮我吗? 感谢。修改
你问我的具体代码,所以:
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<RowItem> rowItems;
CustomAdapter(Context context, ArrayList<RowItem> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(rowItems.get(position));
}
/* private view holder class */
private class ViewHolder {
ImageView profile_pic;
TextView member_name;
TextView status;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.member_name = (TextView) convertView
.findViewById(R.id.member_name);
holder.profile_pic = (ImageView) convertView
.findViewById(R.id.profile_pic);
holder.status = (TextView) convertView.findViewById(R.id.status);
RowItem row_pos = rowItems.get(position);
holder.profile_pic.setImageBitmap(row_pos.getProfile_pic_id());
holder.member_name.setText(row_pos.getMember_name());
holder.status.setText(row_pos.getStatus());
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
答案 0 :(得分:0)
仅当convertView为null时才设置数据。当视图被回收时,你什么也没设置,显示当前重复的数据,试试这个:
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.member_name = (TextView) convertView
.findViewById(R.id.member_name);
holder.profile_pic = (ImageView) convertView
.findViewById(R.id.profile_pic);
holder.status = (TextView) convertView.findViewById(R.id.status);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.profile_pic.setImageBitmap(row_pos.getProfile_pic_id());
holder.member_name.setText(row_pos.getMember_name());
holder.status.setText(rowItems.get(position).getStatus());