1

i am creating a listview having the data from the sqlite database. I ran the program thrice, so there are now 3 records in my database. on running the program i see 3 list rows but the text is not displayed.

here is my Category Activity class

`public class CategoryActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    CategorySQLHelper categorysql = new CategorySQLHelper(this);
    Log.d("insert:", "inserting into database");
    categorysql.addCategory(new Category("Science"));


    //get all category
    Log.d("reading", "reading all category");
    List<Category>category=categorysql.getAllCategory();

    for(Category categories: category){
        String categoryname=categories.getCategory();
        Log.d("categories", categoryname);
    }

    ArrayAdapter<Category> adapter = new ArrayAdapter<Category>(getApplicationContext(), android.R.layout.simple_list_item_1,category);
    setListAdapter(adapter);
}`
Maulik Sheth
  • 584
  • 3
  • 10
  • use activity context `ArrayAdapter adapter = new ArrayAdapter(CateogryActivity.this, android.R.layout.simple_list_item_1,category)` and can you post the snap shot to make it clear – Raghunandan Aug 22 '13 at 12:52
  • Have u checked data is retrieve from data base or what is the size of list – Yogesh Tatwal Aug 22 '13 at 12:53
  • hi thanks @Raghunandan it worked, and now i googled the difference between app context and activity context. i am a little bit more cleared. now i have stumbled upon a problem, on restarting the emulator my list shows only one Science category instead of 3 so i guess my sqllite coding has some problem – Maulik Sheth Aug 22 '13 at 13:07
  • @MaulikSheth check the answer and the link posted you will know more about `getApplicationContext` and when to use it and if it helps don't forget to mark it as accepted. – Raghunandan Aug 22 '13 at 13:13

1 Answers1

0

Use activity context

ArrayAdapter<Category> adapter = new ArrayAdapter<Category>(CateogryActivity.this, android.R.layout.simple_list_item_1,category);
setListAdapter(adapter);

When to call activity context OR application context?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256