2016-12-15 63 views
0

我正在創建我的第一個Android應用程序,希望你能幫助我一下。ListView嵌套在ListView中,不同的行取決於光標值

我想創建不同類型的答案(電臺,複選框或只是打開)的檢查表,現在我知道我需要有一個ListView的問題以及每個問題的嵌套ListView答案。

我試圖做自定義CursorAdapter,這將實現不同的行XML格式根據遊標值不工作對我來說:/但它只是問題的一部分,因爲我仍然不知道如何從另一個表中獲取數據(答案)在(問題)光標內。

有什麼建議嗎?下面我爲不同的XML寫行的代碼(不工作)。

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    if (cursor.getInt(cursor.getColumnIndex(KEY_ID)) <= 3) { 
     return cursorInflater.inflate(R.layout.display_name_row, parent, false); 
    } else { 
     return cursorInflater.inflate(R.layout.display_user_row, parent, false); 
    } 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    TextView textViewTitle = (TextView) view.findViewById(R.id.r_lv_name); 
    TextView textViewTitle2 = (TextView) view.findViewById(R.id.r_lv_u_name); 
    TextView textViewTitle3 = (TextView) view.findViewById(R.id.r_lv_u_surname); 

    if (cursor.getInt(cursor.getColumnIndex(KEY_ID)) <= 3) { 

     String title = cursor.getString(cursor.getColumnIndex(CS_KEY_NAME)); 
     textViewTitle.setText(title); 
    } else { 

     String title = cursor.getString(cursor.getColumnIndex(CS_KEY_NAME)); 
     textViewTitle2.setText(title); 

    } 
} 

編輯1 - 根據DB結果填充childre的函數 - 如何?

public void fillQuestions(int id) { 
    Cursor questionsCursor = db.getQuestions(id); 
    EventActivity.this.startManagingCursor(questionsCursor); 
    questionsCursor.moveToFirst(); 

    ExpandableListView questionsList = (ExpandableListView) findViewById(R.id.elv_questions); 

    ExpandableQuestionsAdapter adapter = new ExpandableQuestionsAdapter(
      questionsCursor, 
      EventActivity.this, 
      R.layout.display_name_row, 
      R.layout.display_cb_answer_row, 
      new String[] {"questionText"}, 
      new int[] {R.id.r_lv_name}, 
      new String[] {"answerName"}, 
      new int[] {R.id.r_lv_cb_name, R.id.r_lv_name}); 

    questionsList.setAdapter(adapter); 
    for(int i=0; i < adapter.getGroupCount(); i++) { 
     questionsList.expandGroup(i); 
    } 

} 

public class ExpandableQuestionsAdapter extends SimpleCursorTreeAdapter { 

    @Override 
    public int getChildTypeCount() { 
     return 2; 
    } 

    @Override 
    public int getChildType(int groupPosition, int childPosition) 
    { 
     int result = 0; 
     if (childPosition == getChildrenCount(groupPosition) - 1) 
      result = 1; 

     return result; 
    } 


    public ExpandableQuestionsAdapter(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); 
    } 

    @Override 
    protected Cursor getChildrenCursor(Cursor groupCursor) { 
     Cursor childCursor = db.getAnswers(groupCursor.getInt(groupCursor.getColumnIndex(KEY_F_ID))); 
     EventActivity.this.startManagingCursor(childCursor); 
     childCursor.moveToFirst(); 
     return childCursor; 

    } 

    @Override 
    protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) { 
     String test = cursor.getString(cursor.getColumnIndex("questionText")); 
     Log.d("Cursor", test); 
     super.bindGroupView(view, context, cursor, isExpanded); 
    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     TextView textView = null; 
     if (convertView == null) { 

      LayoutInflater inflater = (LayoutInflater)getSystemService((getBaseContext().LAYOUT_INFLATER_SERVICE)); 
      int itemType = getChildType(groupPosition, childPosition); 

      switch (itemType) { 
       case 0: 
        convertView = inflater.inflate(R.layout.display_cb_answer_row, null); 
        break; 
       case 1: 
        convertView = inflater.inflate(R.layout.display_name_row, null); 
        break; 
      } 
     } 

     textView = (TextView)convertView.findViewById(R.id.r_lv_name); 
     textView.setText("Hello"); 
     return convertView; 
    } 

    @Override 
    protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) { 
     super.bindChildView(view, context, cursor, isLastChild); 
    } 
} 
+1

嘗試使用'ExpandableListView'。 – Asym

回答

1

嵌套的ListView不是解決方案。 使用

ExpandableListViewhttp://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

創建一個編程LinearLayouthttps://stackoverflow.com/a/5983743/2949782

+0

ExpendableListView聽起來很不錯,但我仍然不知道如何根據數據庫填充不同佈局的孩子。在'.inflate(R.layout.list_item,null);'之前查看'getChildView'中的編輯 – sziszu

+0

;調用'getChildType'來評估來自DB的數據類型,然後膨脹不同的佈局。請參閱http://stackoverflow.com/a/12286542/2949782 –

+0

好吧,我稍微改變了這一點,但仍然不知道如何引用childType與光標,以及它現在沒有綁定任何數據給兒童 - 請參閱編輯1 – sziszu