私はStudentManagerアプリケーションを作成しています。CursorAdapterとクエリに関する問題
MainActivity.javaでは、フローティングボタンをタッチすると、EditActivity.javaに切り替わります。次に、EditActivity.javaでdoneボタンを押すと、sendToMain()はEditActivityからデータを取得し、MainActivityに送信し、DatabaseHandler.javaはデータベースにデータを格納します。
問題は、カーソルを作成してデータベースからリストビューを作成しようとしたときに発生しました。私はDatabaseHandler.javaでreturnCursor()を作成し、MainActivityでそれを使用しました。エラーが発生しました。カーソルのクエリが間違っているのか、StudentCursorAdapter.javaが間違っているのか、あるいはこのエラーが他のものから来ているのか分かりません。
MainActivity.java
public void showStudent()
{
Cursor cursor = databaseHandler.returnCursor();
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); //to make sure cursor can be used
StudentCursorAdapter studentCursorAdapter = new StudentCursorAdapter(this, cursor);
studentCursorAdapter.notifyDataSetChanged();
MlistView1.setAdapter(studentCursorAdapter);
}
DatabaseHandler.java
public Cursor returnCursor()
{
//String selectQuery = "select * from " + TABLE_STUDENTS;
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.query(TABLE_STUDENTS,
new String[] {KEY_IDNUM, KEY_NAME, KEY_BIRTHYEAR, KEY_ISMALE, KEY_CLASSNAME},
null, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
return cursor;
}
StudentCursorAdapter.java
public class StudentCursorAdapter extends CursorAdapter
{
public StudentCursorAdapter(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.custom_listview_layout, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
TextView CtextView1 = (TextView) view.findViewById(R.id.CtextView1);
TextView CtextView2 = (TextView) view.findViewById(R.id.CtextView2);
ImageView CimageView = (ImageView) view.findViewById(R.id.CimageView1);
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
String className = cursor.getString(cursor.getColumnIndexOrThrow("classname"));
CtextView1.setText(name);
CtextView2.setText(className);
}
}
これは、(Androidのメーカー2.2.3で書かれた)私のプロジェクトであります、ブランチ3-8_Fe-6-2017 https://github.com/nhatnguyenduy/StudentManager/tree/3-8_Fe-6-2017
助けてください。ありがとうございました
私はstackoverflowの質問をしたのは初めてですので、私の間違いを許してください。私は自分の質問を編集した –