java - ListView 嵌套在 ListView 中,并且根据 Cursor 值不同行

标签 java android listview android-cursoradapter

我正在创建我的第一个 Android 应用程序,希望您能帮我一点忙。

我想创建包含不同类型答案(单选、复选框或只是打开)的检查表,现在我知道我需要一个 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 - 根据数据库结果填充子项的函数 - 如何操作?

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

最佳答案

嵌套 ListView 不是解决方案。 使用

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

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

关于java - ListView 嵌套在 ListView 中,并且根据 Cursor 值不同行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41164020/

相关文章:

java - JAX-WS (JAXB) : How to marshall java. util.Date 到 xs :anyType as xs:date instead of xs:dateTime?

java - 提取数据并使用 RegEx 格式化它们

java - 如何在 Android Eclipse IDE 中查找/使用 eGit?

安卓 : Multiple Actions on a List View - Focus Issue

android - 我如何检查该项目是否在 arrayList 上?

listview - 从 futurebuilder 中删除项目时,它不会更新

java - 从 SAP 获取数据时 MySQL 服务器版本中的 SQL 语法错误

android - 使用保留的 fragment 会导致错误膨胀 SupportMapFragment?

php - 如何让 Android 应用程序通过互联网与网络服务器通信?

java - Libvirt Java 绑定(bind)定义域