Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否从光标中的多个列填充列表视图,但仅填充一行?_Android - Fatal编程技术网

Android 是否从光标中的多个列填充列表视图,但仅填充一行?

Android 是否从光标中的多个列填充列表视图,但仅填充一行?,android,Android,如果这看起来很愚蠢,我很抱歉,但我对所有这些东西都有点陌生。情况是,我有很多数据存储在数据库中,我需要在列表视图中显示这些数据。第一个视图提取15行,只使用数据库中14列中的两列。我使用此适配器在列表视图中显示: private class CustomListAdapter extends SimpleCursorAdapter { private Cursor cursor; public CustomListAdapter(Context context, int te

如果这看起来很愚蠢,我很抱歉,但我对所有这些东西都有点陌生。情况是,我有很多数据存储在数据库中,我需要在列表视图中显示这些数据。第一个视图提取15行,只使用数据库中14列中的两列。我使用此适配器在列表视图中显示:

private class CustomListAdapter extends SimpleCursorAdapter {

    private Cursor cursor;

    public CustomListAdapter(Context context, int textViewResourceId, Cursor cursor, String from[], int to[]) {
            super(context, textViewResourceId, cursor, from, to);
            this.cursor = cursor;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }
            cursor.moveToPosition(position);
            if (cursor != null) {
                    TextView lt = (TextView) v.findViewById(R.id.lefttext);
                    TextView rt = (TextView) v.findViewById(R.id.righttext);
                    if (lt != null) {
                          lt.setText(/*cursor.getString(cursor.getColumnIndex(EwstableContentProvider.TIMESTAMP))*/cursor.getString(cursor.getColumnIndex(EwstableContentProvider._ID)));                            }
                    if (rt != null){
                          rt.setText(cursor.getString(cursor.getColumnIndex(EwstableContentProvider.TOTALEWS)));
                    }

            }
            return v;
    }
}
}

这甚至可能是愚蠢的,但至少它是有效的。 现在,在下一个活动中,我需要显示来自所有列的数据,但只显示来自用户在上一个活动中选择的行的数据。我想把它放在一个列表视图中,就像来自的列表视图一样,这也是我修改适配器的地方。 这样,我将把数据库中两个字段的数据放入列表视图中的每个项中。这是完美的,它将是一个数据点和一个与之配套的注释。 问题是,此时光标中只有一行,因此@Override之后的位只执行一次,因此我得到一行,而不是列表视图中的7项。
我真的非常感谢任何帮助,即使是以完全不同的方式来做。

更新:对不起,没有仔细阅读问题;见其他答案

您需要使用游标适配器。我推荐(下面的例子)

您还需要将“from”参数更改为要显示的文本的列名(键)。下面是我个人代码中的一个示例。这条线,

new String[] { DBAdapter.KEY_NAME },
这是最重要的。DBAdapter中将其定义为:

public static final String KEY_NAME = "name";
它与我自己数据库中第一列的名称匹配

DBAdapter dba = new DBAdapter(this);
    dba.open();
    Cursor c = dba.list_listMode();

    SimpleCursorAdapter ca = new SimpleCursorAdapter(
            this,
            R.layout.list_item,
            c,
            new String[] { DBAdapter.KEY_NAME },
            new int[] { R.id.list_item_text });

    lv.setTextFilterEnabled(true);
    lv.setAdapter(ca);

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id_long) {
DBAdapter dba=新的DBAdapter(此);
dba.open();
游标c=dba.list_listMode();
SimpleCursorAdapter ca=新SimpleCursorAdapter(
这
R.layout.list_项目,
C
新字符串[]{DBAdapter.KEY_NAME},
新的int[]{R.id.list_item_text});
lv.setTextFilterEnabled(真);
低压设置适配器(ca);
lv.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id(长){

假设您知道列的数量,您可以使用for循环迭代所有列,将每个字符串存储到字符串数组中

String[] arr = new String[cursor.getColumnCount()];
for(int i=0; i < cursor.getColumnCount(); i++)
    arr[i] = cursor.getString(i);
String[]arr=新字符串[cursor.getColumnCount()];
对于(int i=0;i

然后将字符串[]与用于listview的字符串一起使用。

因此,您想要一个数据库行的每一列都作为自己的项目的listview吗?您只需使用
cursor.getColumnCount()
来指定
字符串[]
大小,以及
for
循环的
大小。没问题……和+1用于
ArrayAdapter
,这是我的建议,但你比我快。:)谢谢伙计们,这在一点工作之后起了作用。不过需要注意的是。我必须执行arr[I]=cursor.getString(I);,而不是arr.add(cursor.getString(I));。