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?
use
instad of
use
android.R.id.text1as TextView id if you are using defaultactivity_list_itemlayout for ListView