0

im new to android, and im trying to display data from a SQL database into a ListView seems that I am able to retrieve the data from the de database, but my way of inserting it to the ListView seems incorrect. Log cat throws me the following error:

Caused by: android.content.res.Resources$notFoundException: String resource ID #0x2

if anyone could please look at my code and give me an advice of how can I fix it, I would really apreciate it :), thanks in advance

this is my main activity code

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

 }

my Activity 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>

And my 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>

3 Answers 3

1

The problem is here:

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!

You have getString() instead of c.getString() for the last three.

Sign up to request clarification or add additional context in comments.

1 Comment

And each HasMap map must be add to menuItems ArrayList
0

i guess you have to write your own adapter which extends the Simple adapter. Because you are going to use your own list_item definition. You should have a look on some tutorials of creating a listview. Like this Link Hope could help you with this answer.

Comments

0

the problem in hash map while you putting the values

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

instead of this use below code

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.