9

我的應用程序中有一個sqlite數據庫。我想用這個做一個可擴展的列表視圖。使用本地SQlite數據庫填充可擴展列表視圖的方法

我用我應該採取的方法修復。

試了很多找到相同的教程,但找不到一個,其中一個是用本地數據庫填充Expandable列表。

在android網站中有這樣一個教程,他們用手機中的聯繫人詳細信息填充可展開列表。它們從內容提供商 ExpandableList2.java

這樣所以我的問題是我應該也創造了我的數據庫內容提供商這是我自己的應用程序裏面?

這是唯一的方法還是有其他更好的?

+0

那麼什麼方法最終爲你工作?我也陷入了類似的問題。 – Swayam

+0

你有解決方案嗎? – silverFoxA

回答

3

我已經創建了一個項目嘗試expandablelistview和數據庫,希望這有助於

 
final class ExpAdapter extends CursorTreeAdapter { 
     LayoutInflater mInflator; 

     public ExpAdapter(Cursor cursor, Context context) { 
      super(cursor, context); 
      mInflator = LayoutInflater.from(context); 
     } 

     @Override 
     protected void bindChildView(View view, Context context, Cursor cursor, 
       boolean isLastChild) { 
      TextView tvChild = (TextView) view.findViewById(android.R.id.text1); 
      tvChild.setText(cursor.getString(cursor 
        .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME))); 
     } 

     @Override 
     protected void bindGroupView(View view, Context context, Cursor cursor, 
       boolean isExpanded) { 
      TextView tvGrp = (TextView) view.findViewById(android.R.id.text1); 
      tvGrp.setText(cursor.getString(cursor 
        .getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME))); 
     } 

     @Override 
     protected Cursor getChildrenCursor(Cursor groupCursor) { 
      int groupId = groupCursor.getInt(groupCursor 
        .getColumnIndex(DBHelper.COL_ID)); 
      return aDBHelper.getChildCursor(groupId); 
     } 

     @Override 
     protected View newChildView(Context context, Cursor cursor, 
       boolean isLastChild, ViewGroup parent) { 
      View mView = mInflator.inflate(
        android.R.layout.simple_expandable_list_item_1, null); 
      TextView tvChild = (TextView) mView 
        .findViewById(android.R.id.text1); 
      tvChild.setText(cursor.getString(cursor 
        .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME))); 
      return mView; 
     } 

     @Override 
     protected View newGroupView(Context context, Cursor cursor, 
       boolean isExpanded, ViewGroup parent) { 
      View mView = mInflator.inflate(
        android.R.layout.simple_expandable_list_item_1, null); 
      TextView tvGrp = (TextView) mView.findViewById(android.R.id.text1); 
      tvGrp.setText(cursor.getString(cursor 
        .getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME))); 
      return mView; 
     } 

    } 
+0

非常感謝您的幫助。但看起來像我不能使用CursorApadperTree的組和孩子的自定義視圖。這是真的??對於這個應該使用什麼?我應該使用SimpleCursorTreeAdapter嗎?任何建議 –

+0

我想我也應該使用ExpandableListActivity。在此工作 –

+0

非常感謝您的幫助,這並不難..謝謝噸:) –

2

您不必創建一個內容提供商。

  1. 創建一個DBHelper類並創建函數以從本地數據庫獲取記錄,而無需內容提供者。
  2. 創建一個模型類來保存來自本地數據庫的記錄。
  3. 使用此模型類作爲您的列表適配器的數據。
相關問題