私はアンドロイドが初めてで、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>