Reputation: 4183
I have a custom listview adapter. I can populate it from string arrays, but I should know how many strings there are.
This is Activity with listview:
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);
}
}
Here I must specify the number of elements:
for (int i = 1; i <= 6; i++) {
catalog.add(new ListData(names[i - 1], cost[i - 1], img[i - 1],
desc[i - 1]));
This is ListData activity:
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;
}
}
Also I have AdapterActivity, but I think that I should change something in this two activities. So how should I change the code below to not to declare the number of elements in array? Please, help.
Upvotes: 0
Views: 190
Reputation: 13144
Arrays like String[]
are not designed to do such things. Use List
or ArrayList
instead. You don't have to declare their size.
(If I understood you well and the size of names
shouldn't be constant.)
Upvotes: 1