我需要将数据从数据库显示到listview。我正在使用带有listview的arrayadapter

时间:2014-11-18 06:08:08

标签: android listview android-arrayadapter

这是获取数据的方法。显示"将类型结果更改为字符串"。

public List<String> getData() {
        // TODO Auto-generated method stub
        String[] coloumn = new String[]{KEY,NAME,DETAIL};
        Cursor c=ourDB.query(TABLE_NAME, coloumn, null, null, null, null, null);
        List<String> result =" ";
        List<String> results = new ArrayList<String>();
        int iRow =c.getColumnIndex(KEY);
        int iName =c.getColumnIndex(NAME);
        int iDetails =c.getColumnIndex(DETAIL);
        for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
         result = result + c.getString(iRow)+" "+ c.getString(iName)+" = "+ c.getString(iDetails)+"\n";
             }

        return result;
    }

这是我查看数据的课程。

 public class View extends Activity {

        SimpleCursorAdapter adaptr;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.display);
            ListView lv =(ListView) findViewById(R.id.listView1);
            Remind info = new Remind(this);
            info.open();
            List<String> data =info.getData();
            lv.setAdapter(new ArrayAdapter<String>(this,
                     android.R.layout.simple_list_item_1, data));

        }

 }

请任何人都可以提出任何想法。感谢..!

3 个答案:

答案 0 :(得分:0)

根据您提到的错误,这是一个明显的问题。

getData()中的以下行将字符串文字分配给List。

List<String> result =" ";

将其更改为

String result = " ";

答案 1 :(得分:0)

使用此:

public List<String> getData() {
    // TODO Auto-generated method stub
    String[] coloumn = new String[]{KEY,NAME,DETAIL};
    Cursor c=ourDB.query(TABLE_NAME, coloumn, null, null, null, null, null);
    List<String> result =" ";
    List<String> results = new ArrayList<String>();
    int iRow =c.getColumnIndex(KEY);
    int iName =c.getColumnIndex(NAME);
    int iDetails =c.getColumnIndex(DETAIL);
    for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
     result = result + c.getString(iRow)+" "+ c.getString(iName)+" = "+ c.getString(iDetails)+"\n";
    results.add(result);             
}

    return results;
}

答案 2 :(得分:0)

直接将您的数据库数据添加到List:

results.add(" "+ c.getString(iRow)+" "+ c.getString(iName)+" = "+ c.getString(iDetails)+"\n");

返回字符串列表而不是字符串:

return results;

删除不再需要的代码:

列出结果=&#34; &#34 ;;