BaseAdapter 问题

发布于 2024-10-04 06:15:58 字数 1189 浏览 0 评论 0原文

你好, 我已经扩展了BaseAdapter,我的列表大小是20,但它只显示7条记录,在第7条记录之后,它再次从0开始显示。

public class ContactsAdapter extends 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;
}

帮忙

HI,
i have extended BaseAdapter, my size of the list is 20,but it is showing only 7 records, after the 7th record it is showing again from 0.

public class ContactsAdapter extends BaseAdapter {
private List elements;
private Context 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;
}

}

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听闻余生 2024-10-11 06:15:58

视图会被重用,因此您应该将 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());

Views are reused, so you should put tv_name.setText(t.getFirstName()); after the if-else block:

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文