2017-08-05 65 views
0

我想在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個,它沒有被調用。

回答

2

您需要將textView文本和imageView源設置爲if else語句。您只在視圖的初始化中設置它們。它們在滾動後不會更改,因爲滾動後convertView不爲空。您的適配器將使用過去的convertViews和過去的值。你只需要設置的視圖內容。

@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); 

     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

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()); 

    return convertView; 
} 

編輯:另外一個建議。在類中定義這個變量:

LayoutInflater mInflater; 

稱這種現象爲您的自定義適配器的構造:

mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

和刪除getView方法充氣的初始化。 ListView是非常重要的類。不要厭倦getView方法,因爲這被稱爲每個列表項都在屏幕上。

查看下面的鏈接。這是ListView如何回收的工作原理:

How ListView's recycling mechanism works

+0

這對我有很大的幫助。它的工作,謝謝! – DeGoosseZ