为什么我的自定义游标适配器不起作用?与ArrayAdapter

时间:2017-04-30 16:02:59

标签: java android sqlite

我的程序的目标是将数据存储在SQLite数据库中,然后在列表视图中使用该数据库。 我在下面提供了代码(删除了一些不必要的代码)。 我在数据库中成功插入了一些虚拟数据。 自定义适配器的布局也正常工作。 在创建适应时间表的对象时,我的应用程序崩溃了。 我已经尝试过所有我可以做的事情,但没有工作。

如果需要更多信息,请告诉我。

在MainActivity.java中

    TimeTableDBHelper timeTableDbHelper = new TimeTableDBHelper(this);
    SQLiteDatabase db = timeTableDbHelper.getWritableDatabase();
    db = timeTableDbHelper.getReadableDatabase();
    String[] columns = {TimeTableContract.COLUMN_NAME_START_TIME,TimeTableContract.COLUMN_NAME_END_TIME,
                            TimeTableContract.COLUMN_NAME_DATE,
                            TimeTableContract.COLUMN_NAME_MONTH
                           };

    Cursor cursor = db.query(TimeTableContract.TABLE_NAME, columns, null, null, null, null, null);
//My app is crashing after this line only
    TimeTableAdapter adapter = new TimeTableAdapter(this, cursor);
    ListView listView = (ListView) findViewById(R.id.mainListView1);
   listView.setAdapter(adapter);
cursor.close();

TimetableAdapter.java

public class TimeTableAdapter extends CursorAdapter
{

public TimeTableAdapter(Context context, Cursor cursor)
{
    super(context,cursor,0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{

    return LayoutInflater.from(context).inflate(R.layout.table_list_view, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor)
{
int startColumnIndex = cursor.getColumnIndex(TimeTableContract.COLUMN_NAME_START_TIME);
    String time1 = cursor.getString(startColumnIndex);
    int endColumnIndex = cursor.getColumnIndex(TimeTableContract.COLUMN_NAME_END_TIME);
    String time2 = cursor.getString(endColumnIndex);
    int dateColumnIndex = cursor.getColumnIndex(TimeTableContract.COLUMN_NAME_DATE);
    String date = cursor.getString(dateColumnIndex);
    int monthColumnIndex = cursor.getColumnIndex(TimeTableContract.COLUMN_NAME_MONTH);
    String month = cursor.getString(monthColumnIndex);

    TextView startTime = (TextView) view.findViewById(R.id.start_time);
    TextView endTime = (TextView) view.findViewById(R.id.end_time);
    TextView tuitorName = (TextView) view.findViewById(R.id.tuitor_name);
    TextView subject = (TextView) view.findViewById(R.id.subject);
    TextView remarks = (TextView) view.findViewById(R.id.remarks);
    TextView dateMonth = (TextView) view.findViewById(R.id.date);

    startTime.setText(time1);
    endTime.setText(time2);
    tuitorName.setText("Tutor");
    subject.setText("subject");
    remarks.setText("remarks...");
    dateMonth.setText(date+", " + month);
}

合同类

public  class TimeTableContract 
{
// Make constructor private do that it does not initiatted accidently
private TimeTableContract()
{};
public static final String TABLE_NAME = "time_table";
public static final String COLUMN_NAME_START_TIME = "start_time";
public static final String COLUMN_NAME_END_TIME = "end_time";
public static final String COLUMN_NAME_DATE = "date";
public static final String COLUMN_NAME_MONTH = "month";
public static final String COLUMN_NAME_ID = "_id";

}

通过以下代码在数据库中创建表(顺便提一下我的数据库助手类没有问题)

"CREATE TABLE " + TimeTableContract.TABLE_NAME + " ( " + 
                    TimeTableContract.COLUMN_NAME_ID + " INTEGER PRIMARY KEY," +
                    TimeTableContract.COLUMN_NAME_START_TIME + " TEXT," +
                    TimeTableContract.COLUMN_NAME_END_TIME + " TEXT,"+
                    TimeTableContract.COLUMN_NAME_DATE + " TEXT,"+
                    TimeTableContract.COLUMN_NAME_MONTH + " TEXT)";

1 个答案:

答案 0 :(得分:0)

你错过了" _id"光标栏中的列:

    String[] columns = {TimeTableContract.COLUMN_NAME_START_TIME,
                        TimeTableContract.COLUMN_NAME_END_TIME,
                        TimeTableContract.COLUMN_NAME_DATE,
                        TimeTableContract.COLUMN_NAME_MONTH
                       };

这是一个必须包含" _id"如果您正在使用CursorAdapters和ListViews,则光标中的列,否则它们将无法工作。

当您为listview实现onItemClick()方法时,您将获得与名为long id

的参数相同的_id
   listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, **long id**)