gpt4 book ai didi

android - 如何将 ArrayAdapter 设置为 Listview - Android

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:21 25 4
gpt4 key购买 nike

我有一个扩展 ArrayAdapter 的类。我无法将 ListAdapter 设置为我的 ListView,因为我不知道在尝试创建 ListAdapter 时代码应该是什么。这是我当前的代码。

class Item {
String username;
String number;
String content;

}


class ListAdapter extends ArrayAdapter<Item> {

public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}

private List<Item> items;

public ListAdapter(Context context, int resource, List<Item> items) {

super(context, resource, items);

this.items = items;

}

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

View v = convertView;

if (v == null) {

LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item, null);

}

Item p = items.get(position);

if (p != null) {

TextView tt = (TextView) v.findViewById(R.id.id);
TextView tt1 = (TextView) v.findViewById(R.id.categoryId);
TextView tt3 = (TextView) v.findViewById(R.id.description);

if (tt != null) {
tt.setText(p.getId());
}
if (tt1 != null) {

tt1.setText(p.getCategory().getId());
}
if (tt3 != null) {

tt3.setText(p.getDescription());
}
}

return v;

}
}

这是我遇到问题的地方,试图声明 ListAdapter

List <Item> tempList = new ArrayList <Item>();

ListView yourListView = (ListView) findViewById(android.R.id.list);

// Here is the problem
ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item, List<tempList> //the error occurs right here);

yourListView .setAdapter(customAdapter);

最佳答案

改变

ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item,List<tempList>)

ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item, tempList); 
//...........

删除列表只添加tempList

关于android - 如何将 ArrayAdapter 设置为 Listview - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18372450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com