0

Eclipse 在这条线上给了我错误 ArrayAdapter myAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_2,myArray);

这是代码:

package com.dbapp.activities;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SecondActivity extends Activity {  

int i,j;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        String[][] myArray=new String[10][10];
        ListView myListView=(ListView)findViewById(R.id.lv1);



        CRUD crud=new CRUD(this);
        crud.openDb();

        Cursor cursor= crud.readDbRows();
        cursor.moveToFirst();

        i=j=0;
        while (cursor.moveToNext())
        {
        myArray[i][0]=cursor.getString(cursor.getColumnIndex("column1"));
        myArray[i][1]=cursor.getString(cursor.getColumnIndex("column2"));
        }

        ArrayAdapter<String> myAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2,myArray);
        myListView.setAdapter(myAdapter);
    }

}

这不是主要活动,它只是另一个活动。

4

3 回答 3

1

好吧,似乎 ArrayAdapter 将一维数组作为参数,而不是二维数组。

因此,我将代码更改为一维数组并找到了答案。

我不得不将字符串数组更改为\

ArrayList<String> myArray = new ArrayList<String>();

并使用以下行添加元素:

myArray.add(cursor.getString(cursor.getColumnIndex("name")));

这是java代码

package com.example.databasedemo;

import java.util.ArrayList;

import com.example.databasedemo.CRUD;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.second);


    ArrayList<String> myArray = new ArrayList<String>();
    ListView listv=(ListView)findViewById(R.id.dblist);

    CRUD crud=new CRUD(this);
    crud.openDb();
    Cursor cursor= crud.readDbRows();
    cursor.moveToFirst();


    while(cursor.moveToNext())
    {
        myArray.add(cursor.getString(cursor.getColumnIndex("name")));

    }



    ArrayAdapter<String> myAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myArray);
    listv.setAdapter(myAdapter);

    crud.closeDb();
}
}
于 2013-07-31T16:39:47.617 回答
0

ArrayAdapter 用于一维数组。

在这里看到一个类似的问题 How to load a ListView with a multiple array

希望能帮助到你

于 2013-07-26T07:59:12.520 回答
0

不确定您使用的是哪个 API,但从 API 级别 17 开始,您可能会执行以下操作:

   myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, myArray){
            @Override
            public View getView(int position, View convertView, ViewGroup parent){
                TwoLineListItem row;            
                if(convertView == null){
                    LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    row = (TwoLineListItem)inflater.inflate(android.R.layout.simple_list_item_2, null);                    
                }else{
                    row = (TwoLineListItem)convertView;
                }
                BasicNameValuePair data = list.get(position);
                row.getText1().setText(myArray[position][0]);
                row.getText2().setText(myArray[position][1]);

                return row;
            }
        };
    myListView.setAdapter(myAdapter);
于 2013-07-26T08:09:43.063 回答