0

I'm in the process of creating my first Android app, and hope you can help me a bit.

I want to create check sheets with different type of answers (radio, checkbox or just open), now I know I need to have a ListView of questions as well as nested ListView of answers for every question.

I tried to do custom CursorAdapter that will implement different row XML layout depending on cursor value doesn't work for me :/ but it's just the part of problem as I still don't know how to fetch data from another table (answers) inside of (questions) cursor.

Any suggestions? Below code that I wrote for different XMLs for rows (not working).

@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);

    }
}

Edit 1 - Function to populate childre depending on DB result - how to??

    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);
    }
}
sziszu
  • 490
  • 1
  • 6
  • 16

1 Answers1

1

Nested ListView ins't the solution. Use

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

or

create programmatically a LinearLayout: https://stackoverflow.com/a/5983743/2949782

Community
  • 1
  • 1
  • ExpendableListView sounds very good but I still don't know how to populate childs with different Layouts depending on DB. Look edit – sziszu Dec 22 '16 at 14:26
  • within `getChildView`, before `.inflate(R.layout.list_item, null);` , call `getChildType` for evaluating the type of data from DB, then inflate different layout. See http://stackoverflow.com/a/12286542/2949782 – Vincenzo Petronio Dec 23 '16 at 09:07
  • Ok, I have changed this slightly but still don't know how to refer childType with cursor as well as it's not binding any data now to children - please see edit 1 – sziszu Dec 23 '16 at 11:36