0

私が作成したデータベースからデータを読み取り、カスタム ListView でデータの一部を行に表示しようとしています。何が私の間違いなのか理解できません。

これは私のコードです:

public class EsodaMainActivity extends Activity 
{ 
    public static final String ROW_ID = "row_id"; //Intent extra key
    private ListView esodaListView;  // the ListActivitys ListView
    private SimpleCursorAdapter esodaAdapter; // adapter for ListView
    DatabaseConnector databaseConnector = new DatabaseConnector(EsodaMainActivity.this);


    // called when the activity is first created
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_esoda_main);
        esodaListView = (ListView)findViewById(R.id.esodaList);
        esodaListView.setOnItemClickListener(viewEsodaListener);

        databaseConnector.open();

        //Cursor cursor= databaseConnector.query("esoda", new String[]
        //      {"name", "amount"}, null,null,null);
        Cursor cursor=databaseConnector.getAllEsoda();
        startManagingCursor(cursor);

        // map each esoda to a TextView in the ListView layout
        // The desired columns to be bound
        String[] from = new String[] {"name","amount"}; // built an String array named "from"
        //The XML defined views which the data will be bound to
        int[] to = new int[] { R.id.esodaTextView, R.id.amountTextView}; // built an int array named "to"
        // EsodaMainActivity.this = The context in which the ListView is running
        // R.layout.esoda_list_item = Id of the layout that is used to display each item in ListView
        // null = 
        // from = String array containing the column names to display
        // to = Int array containing the column names to display
        esodaAdapter = new SimpleCursorAdapter (this, R.layout.esoda_list_item, cursor, from, to);
        esodaListView.setAdapter(esodaAdapter); // set esodaView's adapter
    } // end of onCreate method

    @Override
    protected void onResume()
    {
        super.onResume(); // call super's onResume method

        // create new GetEsodaTask and execute it
        // GetEsodaTask is an AsyncTask object
        new GetEsodaTask().execute((Object[]) null);
    } // end of onResume method

    // onStop method is executed when the Activity is no longer visible to the user
    @Override
    protected void onStop()
    {
        Cursor cursor= esodaAdapter.getCursor(); // gets current cursor from esodaAdapter

        if (cursor != null) 
            cursor.deactivate(); // deactivate cursor

        esodaAdapter.changeCursor(null); // adapter now has no cursor (removes the cursor from the CursorAdapter)
        super.onStop();
    } // end of onStop method

    // this class performs db query outside the GUI
    private class GetEsodaTask extends AsyncTask<Object, Object, Cursor>
    {
        // we create a new DatabaseConnector obj
        // EsodaMainActivity.this = Context
        DatabaseConnector databaseConnector = new DatabaseConnector(EsodaMainActivity.this);

        // perform the db access
        @Override
        protected Cursor doInBackground(Object... params)
        {
            databaseConnector.open();

            // get a cursor containing call esoda
            return databaseConnector.getAllEsoda(); 
            // the cursor returned by getAllContacts() is passed to method onPostExecute()
        } // end of doInBackground method

        // here we use the cursor returned from the doInBackground() method
        @Override
        protected void onPostExecute(Cursor result)
        {
            esodaAdapter.changeCursor(result); // set the adapter's Cursor
            databaseConnector.close();
        } // end of onPostExecute() method
    } // end of GetEsodaTask class

    // creates the Activity's menu from a menu resource XML file
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.esoda_menu, menu); // inflates(εμφυσώ) esodamainactivity_menu.xml to the Options menu
        return true;
    } // end of onCreateOptionsMenu() method

    //handles choice from options menu - is executed when the user touches a MenuItem
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // creates a new Intent to launch the AddEditEsoda Activity
        // EsodaMainActivity.this = Context from which the Activity will be launched
        // AddEditEsoda.class = target Activity
        Intent addNewEsoda = new Intent(EsodaMainActivity.this, AddEditEsoda.class);
        startActivity(addNewEsoda);
        return super.onOptionsItemSelected(item);
    } // end of method onPtionsItemSelected()

    // event listener that responds to the user touching a esoda's name in the ListView
    OnItemClickListener viewEsodaListener = new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            // create an intent to launch the ViewEsoda Activity
            Intent viewEsoda = new Intent(EsodaMainActivity.this, ViewEsoda.class);
            // pass the selected esoda's row ID as an extra with the Intent
            viewEsoda.putExtra(ROW_ID, arg3);
            startActivity(viewEsoda); // start viewEsoda.class Activity
        } // end of onItemClick() method
    }; // end of viewEsodaListener

} // end of EsodaMainActivity class

ステートメント:Cursor cursor=databaseConnector.getAllEsoda(); すべてのデータ (列) をクエリします。カスタム ListView で表示したいデータから、そのうちの 2 つは「名前」と「金額」です。しかし、まだデバッガエラーが発生します。助けてください。

デバッガからのメッセージですか?

Esoda [Androidアプリ] DalvikVM[localhost:8603] Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) 行: 2663
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) 行: 2679 ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) 行: 125
ActivityThread$H.handleMessage(メッセージ) 行: 2033
ActivityThread$H(Handler).dispatchMessage(Message) 行: 99 Looper.loop() 行: 123 ActivityThread.main(String[]) 行: 4627 Method.invokeNative(Object, Object[], Class, Class[], Class , int, boolean) 行: 利用不可 [ネイティブ メソッド] Method.invoke(Object, Object...) 行: 521
ZygoteInit$MethodAndArgsCaller.run() 行: 868
ZygoteInit.main(String[]) 行: 626 NativeStart. main(String[]) line: not available [native method] Thread [<6> Binder Thread #2] (Running) Thread [<5> Binder Thread #1] (Running)

LogCat は次のとおりです。

10-20 21:53:19.903: W/ActivityThread(1973): Application development.nk.esoda is waiting for the debugger on port 8100...
10-20 21:53:19.933: I/System.out(1973): Sending WAIT chunk
10-20 21:53:19.943: I/dalvikvm(1973): Debugger is active
10-20 21:53:19.953: I/System.out(1973): Debugger has connected
10-20 21:53:19.953: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:20.215: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:20.478: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:20.673: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:20.890: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:21.133: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:21.333: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:21.573: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:21.790: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:21.995: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:22.198: I/System.out(1973): debugger has settled (1497)
4

1 に答える 1

0

問題は解決しました... getAllContacts() メソッドを呼び出したとき、すべての列を読みませんでした。それを変更し、読み取る列を追加すると、これらの値をカスタム ListView にマップできます

于 2012-10-21T10:30:19.363 に答える