Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayAdapter:you must supply a resource id for a textview

i have an app in which i want to fill the ListView by data from SQLite Database...in this part i have an problem, with arrayAdapter...

here is my code of the method for fill the listView:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    PetListView = (ListView)findViewById(R.id.list);


    MyDatabaseHelper db = new MyDatabaseHelper(this);
    String [] items = new String[100];


    List<Pet> pets = db.getAllPets();
    for (int i = 0; i < 10; i++) {
        items[i] = pets.get(i).getName();                   
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, R.id.textView1list, items);
    PetListView.setAdapter(adapter);

}

and this is method implemented in my database helper:

public List<Pet> getAllPets() {
    List<Pet> petList = new ArrayList<Pet>();
    String selectQuery = "SELECT * FROM " + TABLE_PETS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            Pet pet = new Pet();
            pet.setID(Integer.parseInt(cursor.getString(0)));
            pet.setName(cursor.getString(1));
            pet.setAge(cursor.getString(2));
            petList.add(pet);
        } while (cursor.moveToNext());
    }
    cursor.close();
    return petList;     
}

Why textView? I'm only using ListView...

Can you anybody tell me any other way how to fill the listView by data(names) from SQLite?

like image 956
andrew Avatar asked Dec 08 '12 16:12

andrew


1 Answers

use

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
          android.R.layout.activity_list_item, android.R.id.text1, items);

instad of

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
            android.R.layout.activity_list_item, R.id.textView1list, items);

use android.R.id.text1 as TextView id if you are using default activity_list_item layout for ListView

like image 91
ρяσѕρєя K Avatar answered Oct 21 '22 17:10

ρяσѕρєя K