我有一个扩展arrayAdapter的类。我试图将变量添加到列表中,但我不确定如何这样做。这是我目前的ArrayAdapter类。
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);
}
final Item p = items.get(position);
if (p != null) {
TextView commentView = (TextView)v.findViewById(R.id.listComment);
TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);
commentView.setText(p.content);
NumbersView.setText(p.number);
usernamesView.setText(p.username);
}
return v;
}
}
ListView yourListView = (ListView) findViewById(android.R.id.list);
ListAdapter customAdapter = new ListAdapter(DashboardActivity.this, R.layout.list_item, tempList);
yourListView .setAdapter(customAdapter);
这是我将数据添加到ArrayList的地方,
final List <Item> tempList = new ArrayList <Item>();
String username = json2.getString(KEY_USERNAME);
String number = json2.getString(KEY_NUMBER);
String content = json2.getString(KEY_COMMENT);
//The Error occurs here
tempList.add ( new Item (username, number,content));
错误说The constructor ClipData.Item(String, String, String) is
undefined
答案 0 :(得分:6)
那是因为你没有与该签名匹配的构造函数。只需制作一个。
class Item {
String username;
String number;
String content;
public Item (String username, String number, String content){
this.username = username;
this.number = number;
this.content = content;
}
}