我有一个自定义列表视图适配器。我可以从字符串数组中填充它,但我应该知道有多少个字符串。
这是带有列表视图的活动:
public class MainListActivity extends ListActivity {
private TextView myText;
private ArrayList<ListData> catalog;
String[] names = { some names };
String[] desc = {some description };
int[] cost = { prices };
int[] img = { images };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.titleMain);
catalog = new ArrayList<ListData>();
for (int i = 1; i <= 6; i++) {
catalog.add(new ListData(names[i - 1], cost[i - 1], img[i - 1],
desc[i - 1]));
}
CatalogAdapter catAdapter;
catAdapter = new CatalogAdapter(this, catalog);
setListAdapter(catAdapter);
}
}
在这里我必须指定元素的数量:
for (int i = 1; i <= 6; i++) {
catalog.add(new ListData(names[i - 1], cost[i - 1], img[i - 1],
desc[i - 1]));
这是 ListData 活动:
public class ListData {
String title;
int price;
int image;
String discribe;
ListData(String _title, int _price, int _image, String _discribe) {
title = _title;
price = _price;
image = _image;
discribe = _discribe;
}
}
我也有 AdapterActivity,但我认为我应该在这两个活动中改变一些东西。那么我应该如何将下面的代码更改为不声明数组中的元素数量?请帮忙。