0

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

私の質問:

カテゴリごとにリストを作成する必要はありませんか?1つのリストで試しましたが、結果はあまり良くありません。すべてのカテゴリに同じアイテムが表示され、個々のアイテムは表示されません。これは理にかなっています。

4

2 に答える 2

0

自分でリストを作成する必要はありません。

2つのテーブルが必要になる場合があります。1つはカテゴリ名とそのID(プライマリテーブルキーになります)を一覧表示し、もう1つは各行にアイテムのカテゴリID、アイテム名などを1つのアイテムに含むものを一覧表示します。

次に、次SimpleCursorTreeAdapterのようにすることができます。

SimpleCursorTreeAdapter (Context context, Cursor cursor, int groupLayout, String[] groupFrom, int[] groupTo, int childLayout, String[] childFrom, int[] childTo).

groupLayoutあなたのためにandroid.R.layout.simple_expandable_list_item_1childLayoutあなたのためにandroid.R.layout.simple_list_item_1

groupFromあなたのために

String [] groupFrom = {FLD_CATEGORY_NAME}; int [] groupTo = {android.R.id.text1};

childFromからあなたは持っているでしょう

String[] childFrom = { FLD_ITEM_NAME };
int[] childTo = { android.R.id.text1 };

1つまたは2つ以上の要素を表示する場合は、独自のレイアウトを使用します

そして、あなたは

protected Cursor getChildrenCursor(Cursor groupCursor) {
      int categoryId = groupCursor.getColumnIndex(FLD_CATEGORY_ID);
      // and here you a Cursor from your items table WHERE CTAEGORY_ID == categoryId
}
于 2012-07-27T19:06:58.140 に答える
0

リストのリストを作成して、作業しました:)

于 2012-07-31T17:31:57.690 に答える