0

こんにちは、sqlite データベースからリストビューにデータを取得しようとしています。すでにいくつかの方法を試しましたが、常にエラーが発生します。(そして、はい、私はすでにその行のコードを修正しようとしましたが、失敗しました)。コード:

        Cursor cursor = db.getAllmeds();

        String[] list = null;
        int i = 0;
        cursor.moveToFirst();
        for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
            list[i]=cursor.getString(cursor.getColumnIndex("medicamentos_desig"));
            i+=1;
        }

        int to[] = {R.id.listView1};

        db.close();
        final ListView listview = (ListView) findViewById(R.id.listView1);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.medicamentos,cursor,list,to,0);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

                Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);

                String id = listCursor.getString(listCursor.getColumnIndex("medicamentos_id"));

                Intent passIntent = new Intent(arg1.getContext(),Editar.class);
                passIntent.putExtra("id", id);
                passIntent.putExtra("tipo", tipo);
                passIntent.putExtra("accao", "update");
                startActivity(passIntent);
            }

        });

エラーログ:

     05-29 18:33:32.653: E/AndroidRuntime(341): java.lang.ClassCastException: java.lang.String
     05-29 18:33:32.653: E/AndroidRuntime(341):     at admed.Listar$1.onItemClick(Listar.java:55)
4

1 に答える 1

0

ここに:

String[] list = null;  //<<<

list要素を追加する前に Arrayを初期化する必要があります。

 cursor.moveToFirst();
 list=new String[cursor.getCount()];//<<<initialize Array here with cursor Count
 .....
于 2013-05-29T17:44:47.687 に答える