我正在開發一個應用程序爲的Android有沒有更好的方式來處理這個字符串列表?
我有不同的類別:
String[] cat = { "Category", "Books", "Clothing", "Shoes", "Hobbies",
"General", "Electronics", "Transportation", "Medicine",
"Sports", "Education", "Outdoor"};
的UI讓用戶選擇一個類別,名稱,以及一些介紹,然後將其寫入到數據庫。當用戶按下列表按鈕時,將顯示填充的每個類別,並且該類別下的項目也使用ExpandableListActivity顯示。
List<List<String>> children = new ArrayList<List<String>>();
/**********************
I have a list for each category!!! But I don't like this solution. :(
**********************/
List<String> books = new ArrayList<String>();
List<String> clothing = new ArrayList<String>();
...
...
public void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.fetchAllItems();
startManagingCursor(c);
// for all rows
for(int i=0; i<c.getCount(); i++)
{
// next row
c.moveToNext();
// pick a category
String t = c.getString(c.getColumnIndex("category"));
switch (getCat(t))
{
case Books:
books.add(c.getString(c.getColumnIndex("name")));
break;
case Clothing:
// do stuff for Clothing
break;
/**
And there are other cases for the rest of the categories but i am not listing
them here, since it is the same process
*/
default:
break;
} // end switch
} // end for loop
//for each category that has been filled up
children.add(category);
} // end function
我的問題:
是有可能不必爲每個類別的清單?我嘗試了一個列表,但結果看起來不太好。所有類別顯示相同的項目,而不是單個項目,這是有道理的。
不要這樣想。清單列表似乎是在不過度複雜的情況下構建數據的最佳方式。 – 2012-07-27 18:57:22