我正在创建我的第一个 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/