自定义ListView onitemClick中的错误

时间:2012-03-07 10:28:42

标签: android android-listview

我正在使用自定义listView来显示来自sqlite的数据。项目的详细信息将显示在listItem上单击。所以,我试图在ListItemClicked上设置动作,然后我得到了一些错误,如

database not Open and arrayIdexOutOfBound

然后我在行上设置了一个点击监听器(列表项的视图)。它工作正常。但如果我立即点击,我会收到如下错误:

03-07 17:11:51.223: E/AndroidRuntime(789): FATAL EXCEPTION: main
03-07 17:11:51.223: E/AndroidRuntime(789): java.lang.IllegalStateException: database not open
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1224)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
03-07 17:11:51.223: E/AndroidRuntime(789):  at (WeeklyDataSource.java:101)
03-07 17:11:51.223: E/AndroidRuntime(789):  at l(WeeklyActivity.java:112)
03-07 17:11:51.223: E/AndroidRuntime(789):  at .getCount(WeeklyActivity.java:120)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:157)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.widget.ListView.dispatchDraw(ListView.java:2991)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.View.draw(View.java:6846)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.widget.AbsListView.draw(AbsListView.java:2257)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.View.draw(View.java:6743)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.View.draw(View.java:6743)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.View.draw(View.java:6743)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
03-07 17:11:51.223: E/AndroidRuntime(789):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.ViewRoot.draw(ViewRoot.java:1407)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.os.Looper.loop(Looper.java:123)
03-07 17:11:51.223: E/AndroidRuntime(789):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-07 17:11:51.223: E/AndroidRuntime(789):  at java.lang.reflect.Method.invokeNative(Native Method)
03-07 17:11:51.223: E/AndroidRuntime(789):  at java.lang.reflect.Method.invoke(Method.java:521)
03-07 17:11:51.223: E/AndroidRuntime(789):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-07 17:11:51.223: E/AndroidRuntime(789):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-07 17:11:51.223: E/AndroidRuntime(789):  at dalvik.system.NativeStart.main(Native Method)

我不是任何专家或经验丰富的人。我只是不明白这个错误。

我设置row.onclickListener

的代码
row.setOnClickListener(new View.OnClickListener()
            {

                public void onClick(View v)
                {
                    Gson gson = new Gson();
                    WeeklySchedule schedule = (WeeklySchedule) getItem(position);

                    String string = gson.toJson(schedule);
                    Intent intent = new Intent(WeeklyActivity.this,
                            AddWeeklySchedule.class);
                    intent.putExtra("GSON", string);
                    startActivity(intent);
                }
            });

以下是DataSource的代码:

public class WeeklyDataSource
    {
        private static final String TAG = "WeeklyDataSource";

    private SQLiteDatabase weeklydb;
    private WeeklySqliteOpenHelper dbHelper;
    private Cursor cursor;

    // private Context context;

    public WeeklyDataSource(Context context)
    {
        // this.context = context;
        dbHelper = new WeeklySqliteOpenHelper(context);
    }

    public void open() throws SQLException
    {
        weeklydb = dbHelper.getWritableDatabase();
    }

    public void close()
    {
        weeklydb.close();
    }

    public boolean createSchedule(WeeklySchedule weekly)
    {
        ContentValues cv = new ContentValues();
        int i = 1;
        String[] s = WeeklySqliteOpenHelper.getAllcoulumn();
        cv.put(s[i++], weekly.getStartDate());
        cv.put(s[i++], weekly.getEndDate());
        cv.put(s[i++], weekly.getMondayHour());
        cv.put(s[i++], weekly.getTuesdayHour());
        cv.put(s[i++], weekly.getWednesdayHour());
        cv.put(s[i++], weekly.getThusdayHour());
        cv.put(s[i++], weekly.getFridayHour());

        weeklydb.insert(WeeklySqliteOpenHelper.getWeeklyTableName(), null, cv);

        // Cursor cursor = weeklydb.query(
        // WeeklySqliteOpenHelper.getWeeklyTableName(), s, s[0] + " = "
        // + insertId, null, null, null, null);
        // cursor.moveToFirst();
        // cursorToWeeklySchedule(cursor);
        return true;
    }

    public boolean updateSchedule(WeeklySchedule weekly)
    {
        ContentValues cv = new ContentValues();
        int i = 1;
        cv.put(WeeklySqliteOpenHelper.getAllcoulumn()[i++],
                weekly.getStartDate());
        cv.put(WeeklySqliteOpenHelper.getAllcoulumn()[i++], weekly.getEndDate());
        cv.put(WeeklySqliteOpenHelper.getAllcoulumn()[i++],
                weekly.getMondayHour());
        cv.put(WeeklySqliteOpenHelper.getAllcoulumn()[i++],
                weekly.getTuesdayHour());
        cv.put(WeeklySqliteOpenHelper.getAllcoulumn()[i++],
                weekly.getWednesdayHour());
        cv.put(WeeklySqliteOpenHelper.getAllcoulumn()[i++],
                weekly.getThusdayHour());
        cv.put(WeeklySqliteOpenHelper.getAllcoulumn()[i++],
                weekly.getFridayHour());
        weeklydb.update(
                WeeklySqliteOpenHelper.getWeeklyTableName(),
                cv,
                WeeklySqliteOpenHelper.getAllcoulumn()[0] + "="
                        + weekly.get_id(), null);
        return true;
    }

    public boolean deleteSchedule(WeeklySchedule schedule)
    {
        System.out.println("Subject deleted with id: " + schedule.get_id());
        weeklydb = dbHelper.getWritableDatabase();
        weeklydb.delete(WeeklySqliteOpenHelper.getWeeklyTableName(),
                SubjectSqliteOpenHelper.KEY_ROWID + " = " + schedule.get_id(),
                null);
        return false;
    }

    public ArrayList<WeeklySchedule> getAllSchedule()
    {
        ArrayList<WeeklySchedule> allSchedule = new ArrayList<WeeklySchedule>();
        cursor = weeklydb.query(WeeklySqliteOpenHelper.getWeeklyTableName(),
                WeeklySqliteOpenHelper.getAllcoulumn(), null, null, null, null,
                null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast())
        {
            allSchedule.add(cursorToWeeklySchedule(cursor));
            cursor.moveToNext();
        }
        cursor.close();
        return allSchedule;
    }

    public WeeklySchedule cursorToWeeklySchedule(Cursor cursor)
    {
        WeeklySchedule schedule = new WeeklySchedule(cursor.getString(1),
                cursor.getString(2), cursor.getString(3), cursor.getString(4),
                cursor.getString(5), cursor.getString(6), cursor.getString(7));
        schedule.set_id(cursor.getLong(0));
        return schedule;
    }
}

我的问题是为什么会发生这种情况(错误)?

任何解决此错误的想法都会非常有用。

感谢。

4 个答案:

答案 0 :(得分:1)

这种错误只有一个原因。我在自定义适配器中使用了arrayList(它具有listView的值)。

只需将arrayList设为全局并加载onResume()中的所有值即可解决问题。就是这样,我再也没有遇到过arrayOutOfBound错误。

答案 1 :(得分:0)

如果您使用BD来填充ListView,则需要打开它并在列表显示在屏幕之前使用光标,例如Activity(或ListActivity)的onResume()方法。

使用SQLiteOpenHelper

的getRedableDatabase()

更多信息:http://developer.android.com/guide/topics/data/data-storage.html

在onPause()方法之前不必关闭光标。

答案 2 :(得分:0)

如果我正确理解了您的问题,那么当您单击ListView中的某行时,您希望发生某些事情。以下是我为这种情况所做的,我在ListView上设置onItemClickListener

myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
      public void onItemClick(AdapterView<?> av, View view, int position, long arg3) {
      //do your processing on the item here based on the position variable

有关详细信息,请查看此示例:http://www.vogella.de/articles/AndroidListView/article.html#listview_adapterlistener

答案 3 :(得分:0)

我认为你在“WeeklyDtaSource”课程

中遗漏了这一部分
public class WeeklyDataSource {
.....
public void open() throws SQLException{
weeklydb =WeeklySqliteOpenHelper.getWritableDtabase();
}
.....
}