0

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

3 に答える 3

1

問題はここにあります:

map.put(KEY_HOLE, c.getString(1)); // correct
map.put(KEY_PAR, getString(2)); // incorrect!
map.put(KEY_HANDICAP, getString(3)); // incorrect!
map.put(KEY_YARDS,getString(4)); // incorrect!

あなたは最後の3つのgetString()代わりに持っています。c.getString()

于 2013-07-31T19:20:06.570 に答える
0

値を入れている間のハッシュマップの問題

  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));

以下のコードを使用する代わりに

 HashMap<String, String> map = new HashMap<String,    String>();
          map.put(KEY_HOLE, c.getString(1));
          map.put(KEY_PAR, c.getString(2));
          map.put(KEY_HANDICAP, c.getString(3));
          map.put(KEY_YARDS,c.getString(4));
于 2013-07-31T19:32:45.533 に答える
0

Simple アダプターを拡張する独自のアダプターを作成する必要があると思います。独自の list_item 定義を使用するためです。リストビューの作成に関するいくつかのチュートリアルをご覧ください。このリンク 希望のように、この回答を得るのに役立ちます。

于 2013-07-31T19:20:53.833 に答える