我想在Android中使用listview查看whatsapp中的聊天方式。我在我的Fragment中有一個listview,我從Firebase獲取數據放入列表中。前10項非常好,但在10之後,getview()函數不再被調用。我得到錯誤文本的按鈕(來自其他組的名稱),但如果點擊它們,則會在其中顯示正確的名稱。Listview的BaseAdapter沒有調用索引後的所有項目getView
代碼片段:
片段:
rowItems = new ArrayList<RowItem>();
final CustomAdapter adapter = new CustomAdapter(getContext(), rowItems);
mylistview = (ListView) rootView.findViewById(R.id.LVGroups);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(this);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue(String.class);
Log.d("TAG", "name: " + name);
//Create new item in the rowItems arraylist to add to the listview
RowItem item = new RowItem(name, R.mipmap.ic_challenger_black, "placeholder");
rowItems.add(item);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
RowItem:
public class RowItem {
private String member_name;
private int profile_pic_id;
private String status;
public RowItem(String member_name, int profile_pic_id, String status) {
this.member_name = member_name;
Log.d("TAG", "member_name: " + this.member_name);
this.profile_pic_id = profile_pic_id;
this.status = status;
}
public String getMember_name() {
return this.member_name;
}
public void setMember_name(String member_name) {
this.member_name = member_name;
}
public int getProfile_pic_id() {
return profile_pic_id;
}
public void setProfile_pic_id(int profile_pic_id) {
this.profile_pic_id = profile_pic_id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
CustomAdapter:
public class CustomAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
CustomAdapter(Context context, List<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(getItem(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 = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_groups, null);
holder = new ViewHolder();
holder.member_name = (TextView) convertView
.findViewById(R.id.name);
holder.profile_pic = (ImageView) convertView
.findViewById(R.id.pic);
holder.status = (TextView) convertView.findViewById(R.id.sta);
RowItem row_pos = rowItems.get(position);
holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
holder.member_name.setText(row_pos.getMember_name());
Log.d("TAG", "holder.member_name: " + row_pos.getMember_name() + " " + position);
holder.status.setText(row_pos.getStatus());
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
在customadapter中打印直到第10個元素(索引9)。對於最後2個,它沒有被調用。
這對我有很大的幫助。它的工作,謝謝! – DeGoosseZ