我是 android 新手,我试图将 SQL 数据库中的数据显示到 ListView 中似乎我能够从 de 数据库中检索数据,但我将其插入 ListView 的方式似乎不正确。日志猫向我抛出以下错误:
Caused by: android.content.res.Resources$notFoundException: String resource ID #0x2
如果有人可以查看我的代码并给我一个关于如何修复它的建议,我真的很感激:),提前谢谢
这是我的主要活动代码
public class MainActivity extends ListActivity {
String KEY_ID = "_id";
String KEY_HOLE = "hoyo";
String KEY_PAR = "par";
String KEY_HANDICAP = "handicap";
String KEY_YARDS = "yardaje";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
Cursor c = myDbHelper.getAllCourseContacts();
if(c.moveToFirst())
{
do{
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_HOLE, c.getString(1));
map.put(KEY_PAR, getString(2));
map.put(KEY_HANDICAP, getString(3));
map.put(KEY_YARDS,getString(4));
}while(c.moveToNext());
}
myDbHelper.close();
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_HOLE, KEY_PAR, KEY_HANDICAP, KEY_YARDS }, new int[] {
R.id.hole, R.id.par, R.id.handicap,R.id.yards });
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我的活动 XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
</LinearLayout>
还有我的 item_list xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/hole"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize = "40sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/par"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<TextView
android:id="@+id/handicap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<TextView
android:id="@+id/yards"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>