ListView嵌套在ListView中,不同的行取决于Cursor值

时间:2016-12-15 12:19:11

标签: java android listview android-cursoradapter

我正在创建我的第一个Android应用,希望你能帮到我一点。

我想创建包含不同答案类型的检查表(收音机,复选框或只是打开),现在我知道我需要ListView个问题以及嵌套ListView的答案每个问题。

我尝试做自定义CursorAdapter,根据游标值实现不同的行XML布局对我不起作用:/但它只是问题的一部分,因为我仍然没有&# 39;知道如何从(问题)光标内的另一个表(答案)中获取数据。

有什么建议吗?下面我为不同的行编写的代码(不工作)。

@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 - 根据数据库结果填充子节点的函数 - 如何??

    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 个答案:

答案 0 :(得分:1)

嵌套ListView解决方案。 使用

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

以编程方式创建LinearLayouthttps://stackoverflow.com/a/5983743/2949782