我正在尝试使用 cursorAapter 将两个字符串绑定到 textView。但是,我似乎得到了资源未找到异常。我不明白为什么,因为它们都是 2 种字符串类型,而且我在程序的开头输入了一个条目,所以会有一些东西要显示。
日志猫
10-21 19:44:51.042: E/AndroidRuntime(14930): Uncaught handler: thread main exiting due to uncaught exception
10-21 19:44:51.062: E/AndroidRuntime(14930): android.content.res.Resources$NotFoundException: String resource ID #0x1
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.content.res.Resources.getText(Resources.java:200)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.TextView.setText(TextView.java:2813)
10-21 19:44:51.062: E/AndroidRuntime(14930): at com.example.mycoursetimetable.TestCursorAdapter.bindView(TestCursorAdapter.java:28)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.ListView.makeAndAddView(ListView.java:1668)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.ListView.fillDown(ListView.java:637)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.ListView.fillFromTop(ListView.java:694)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.ListView.layoutChildren(ListView.java:1521)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.view.View.layout(View.java:6830)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.view.View.layout(View.java:6830)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.view.View.layout(View.java:6830)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.view.View.layout(View.java:6830)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.view.View.layout(View.java:6830)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.os.Looper.loop(Looper.java:123)
10-21 19:44:51.062: E/AndroidRuntime(14930): at android.app.ActivityThread.main(ActivityThread.java:4363)
10-21 19:44:51.062: E/AndroidRuntime(14930): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 19:44:51.062: E/AndroidRuntime(14930): at java.lang.reflect.Method.invoke(Method.java:521)
10-21 19:44:51.062: E/AndroidRuntime(14930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-21 19:44:51.062: E/AndroidRuntime(14930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-21 19:44:51.062: E/AndroidRuntime(14930): at dalvik.system.NativeStart.main(Native Method)
10-21 19:44:51.082: I/dalvikvm(14930): threadid=7: reacting to signal 3
主要活动
public class MyCourses extends Activity {
static final String TEST = "com.example.mycoursetimetable.TEST";
database MODULEDATABASE;
ListView listContent;
Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_courses);
listContent =(ListView)findViewById(R.id.list);
MODULEDATABASE = new database(MyCourses.this);
MODULEDATABASE.createEntry("TestCode", "TestName");
MODULEDATABASE.openToRead(); // don't forget this part!
cursor = MODULEDATABASE.getData();
listContent = (ListView)findViewById(R.id.list);
TestCursorAdapter ca = new TestCursorAdapter(this, cursor, 0);
listContent.setAdapter(ca);
}
}
光标适配器
public class TestCursorAdapter extends CursorAdapter {
private LayoutInflater viewInflater;
public TestCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
// TODO Auto-generated constructor stub
viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View v, Context context, Cursor c) {
TextView text_moduleId = (TextView)v.findViewById(R.id.labelDay);
TextView text_modulecode = (TextView)v.findViewById(R.id.labelModuleCode);
TextView text_modulename = (TextView)v.findViewById(R.id.labelEditModuleFull);
//text_moduleId.setText(c.getString(c.getColumnIndex(database.KEY_ROWID)));
text_modulecode.setText(c.getColumnIndex(database.KEY_MODULECODE));
text_modulename.setText(c.getColumnIndex(database.KEY_MODULENAME));
}
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
View v = viewInflater.inflate(R.layout.listcourses, parent, false);
return v;
}
}
数据库片段
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_MODULECODE + " TEXT, " +
KEY_MODULENAME + " TEXT); "
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABEL IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public database(Context c){
myContext = c;
}
public database openToWrite()throws SQLException{
myHelper = new DbHelper(myContext);
moduleDatabase = myHelper.getWritableDatabase();
return this;
}
public database openToRead()throws SQLException{
myHelper = new DbHelper(myContext);
moduleDatabase = myHelper.getReadableDatabase();
return this;
}
public void close(){
myHelper.close();
}
public long createEntry(String moduleCode, String moduleName) {
ContentValues cv = new ContentValues();
cv.put(KEY_MODULECODE,moduleCode);
cv.put(KEY_MODULENAME,moduleName);
return moduleDatabase.insert(DATABASE_TABLE,null,cv);
// TODO Auto-generated method stub
}
public Cursor getData() {
String[] columns = new String [] {KEY_ROWID,KEY_MODULECODE, KEY_MODULENAME};
Cursor c = moduleDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
return c;
}
}