2010-02-14 11 views
9

मैं एंड्रॉइड के लिए एक साधारण एप्लीकेशन लिख रहा हूं।एंड्रॉइड एक्सपेंडेबल लिस्टएक्टिविटी और सरल कर्सर ट्रीएडाप्टर?

मेरे पास 2 टेबल हैं - जिन्हें 'ग्रास' कहा जाता है और दूसरा 'group_items' कहा जाता है।

मैं दोनों टेबलों से डेटा प्रदर्शित करने के लिए विस्तारणीय सूची का उपयोग करना चाहता हूं।

ऐसा करने का सबसे अच्छा तरीका क्या है? क्या SimpleCursorTreeAdapter का उपयोग करके डेटा मैप करना संभव है? मुझे कोई उपयोगी उदाहरण नहीं मिला।

मैंने उदाहरणों को ArrayAdapter का उपयोग करके विस्तारणीय सूचियां बनाने के लिए देखा, तो क्या मुझे पहले डेटा को सरणी में परिवर्तित करना चाहिए और उसके बाद एक विस्तार योग्य सूची बनाना चाहिए या इसे सीधे करने का तरीका है?

मुझे एक पूर्ण कामकाजी उदाहरण की आवश्यकता नहीं है - केवल यह सलाह है कि इसे करने का सही और सबसे प्रभावी तरीका क्या है।

Leonti

+0

तुम यहाँ एक नज़र हुई है: यहाँ कोड उदाहरण (महत्वपूर्ण भागों) है http://developer.android.com/resources/samples/ApiDemos/ src/com/example/android/apis/view/list6.html? –

+0

@ लिओन्टी: तो क्या आप समाधान के साथ आ सकते हैं? यदि हां तो कृपया इसे साझा करें। यह बहुत उपयोगी होगा क्योंकि कोई अच्छा उदाहरण उपलब्ध नहीं है। धन्यवाद –

+0

मैं यहां कुछ ऐसा कर रहा हूं http://stackoverflow.com/questions/10611927/simplecursortreeadapter-and-cursorloader – toobsco42

उत्तर

19

मैंने पाया कि सबसे सरल समाधान SimpleCursorTreeAdapter उपयोग करने के लिए किया जाएगा।

public class ExercisesList extends ExpandableListActivity { 


private ExcercisesDbAdapter mDbHelper; // your db adapter 
private Cursor mGroupsCursor; // cursor for list of groups (list top nodes) 
private int mGroupIdColumnIndex; 
private MyExpandableListAdapter mAdapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mDbHelper = new ExcercisesDbAdapter(this); 
     mDbHelper.open(); 
     fillData(); 
} 

private void fillData() { 
     mGroupsCursor = mDbHelper.fetchAllGroups(); // fills cursor with list of your top nodes - groups 
     startManagingCursor(mGroupsCursor); 

     // Cache the ID column index 
     mGroupIdColumnIndex = mGroupsCursor 
         .getColumnIndexOrThrow(ExcercisesDbAdapter.KEY_ROWID); 

     // Set up our adapter 
     mAdapter = new MyExpandableListAdapter(mGroupsCursor,this, 

         android.R.layout.simple_expandable_list_item_1, 
         R.layout.exercise_list_row, 

         new String[] { ExcercisesDbAdapter.KEY_TITLE }, // group title for group layouts 
         new int[] { android.R.id.text1 }, 

         new String[] { ExcercisesDbAdapter.KEY_TITLE }, // exercise title for child layouts 
         new int[] { R.id.exercise_title }); 

     setListAdapter(mAdapter); 
} 

// extending SimpleCursorTreeAdapter 
public class MyExpandableListAdapter extends SimpleCursorTreeAdapter { 

     public MyExpandableListAdapter(Cursor cursor, Context context, 
         int groupLayout, int childLayout, String[] groupFrom, 
         int[] groupTo, String[] childrenFrom, int[] childrenTo) { 
       super(context, cursor, groupLayout, groupFrom, groupTo, 
           childLayout, childrenFrom, childrenTo); 
     } 

     // returns cursor with subitems for given group cursor 
     @Override 
     protected Cursor getChildrenCursor(Cursor groupCursor) { 
       Cursor exercisesCursor = mDbHelper 
           .fetchExcercisesForGroup(groupCursor 
               .getLong(mGroupIdColumnIndex)); 
       startManagingCursor(exercisesCursor); 
       return exercisesCursor; 
     } 

     // I needed to process click on click of the button on child item 
     public View getChildView(final int groupPosition, 
         final int childPosition, boolean isLastChild, View convertView, 
         ViewGroup parent) { 
       View rowView = super.getChildView(groupPosition, childPosition, 
           isLastChild, convertView, parent); 

       Button details = (Button) rowView.findViewById(R.id.view_button); 

       details.setOnClickListener(new OnClickListener() { 
         public void onClick(View v) { 

           Cursor exerciseCursor = getChild(groupPosition, childPosition); 

           Long exerciseId = exerciseCursor.getLong(exerciseCursor.getColumnIndex(ExcercisesDbAdapter.KEY_ROWID)); 

           Intent i = new Intent(ExercisesList.this, ExerciseView.class); 
           i.putExtra(ExcercisesDbAdapter.KEY_ROWID, exerciseId); 
           startActivity(i); 
         } 
       }); 

       return rowView; 
     } 

} 

} 

आशा है कि यह उपयोगी होगा;)

+0

हमारे लिए सामग्री प्रदाताओं से बचने का प्रयास करने के लिए बहुत उपयोगी है। –

+0

महोदय, क्या आप कृपया मेरे प्रश्न का उत्तर भी दे सकते हैं http://stackoverflow.com/questions/29457424/expandablelistview-extends-simplecursoradapter-to-populate-from-sqlite – silverFoxA

संबंधित मुद्दे