【问题标题】:Problem with BaseAdapterBaseAdapter 的问题
【发布时间】:2010-11-23 05:05:19
【问题描述】:

嗨, 我扩展了 BaseAdapter,我的列表大小是 20,但它只显示 7 条记录,在第 7 条记录之后它又从 0 开始显示。

公共类 ContactsAdapter 扩展 BaseAdapter { 私有列表元素; 私有上下文 c;

public ContactsAdapter(Context c, List<ContactBean> Tweets) {
    this.elements = Tweets;
    this.c = c; }

public int getCount() {
    return elements.size(); }

public Object getItem(int position) {
    System.out.println(":: ::"+position);
    System.out.println("Printing Postions ::"+elements.get(position));
    return elements.get(position);  }

public long getItemId(int id) {
    return id;  }

public void Remove(int id) {        notifyDataSetChanged(); }

public View getView(int position, View convertView, ViewGroup parent) {

    LinearLayout rowLayout;
    ContactBean t = elements.get(position);

    if (convertView == null) {
        rowLayout = (LinearLayout) LayoutInflater.from(c).inflate(
                R.layout.list_view, parent, false);
        TextView tv_name = (TextView) rowLayout.findViewById(R.id.txt_contacts_name);
        tv_name.setText(t.getFirstName());
    } else {
        rowLayout = (LinearLayout) convertView;
    }
    return rowLayout;
}

}

请帮忙

【问题讨论】:

    标签: android adapter base


    【解决方案1】:

    视图被重用,所以你应该把tv_name.setText(t.getFirstName());放在 if-else 块之后:

    if (convertView == null) {
        rowLayout = (LinearLayout) LayoutInflater.from(c).inflate(R.layout.list_view, parent, false);
    } else {
        rowLayout = (LinearLayout) convertView;
    }
    
    TextView tv_name = (TextView) rowLayout.findViewById(R.id.txt_contacts_name);
    tv_name.setText(t.getFirstName());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-25
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多