Reputation: 656
here is the code to get all categories from a function in databaseadapter class:
public List<CategoryClass> getAllCategory()
{
List<CategoryClass> getCategoryList = new ArrayList<CategoryClass>();
dataAdapter = dataAdapter.openToWrite();
Cursor cursor = db.rawQuery("SELECT * FROM " + tb_Name,null);
if (cursor.moveToFirst()) {
do {
CategoryClass categoryInstance = new CategoryClass();
categoryInstance.setCategory(cursor.getString(1));
// Adding contact to list
getCategoryList.add(categoryInstance);
cursor.close();
}
while (cursor.moveToNext());
}
Log.d("retrieving all categories","count < 0");
return getCategoryList;
}
now need to display values in list view when activity start my app should show all values of db into listview, here is the code:
public class Categories extends Activity
{
DatabaseAdapter dataAdapterInstance;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_list);
ListView list = (ListView) findViewById(R.id.listView);
dataAdapterInstance = new DatabaseAdapter(this);
dataAdapterInstance = dataAdapterInstance.openToWrite();
List<CategoryClass> category = dataAdapterInstance.getAllCategory();
for (CategoryClass cn : category) {
cn.getCategory();
category.add(cn);
ArrayAdapter<CategoryClass> arrayAdapter = new ArrayAdapter<CategoryClass>(this,android.R.layout.simple_list_item_1,category);
list.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
}
but getting null pointer exception when trying to click on button and open this activity which contains above code: pls let me know how can i resolve this issue here is the stacktrace:
06-11 20:46:09.531: W/System.err(17223): java.lang.NullPointerException
06-11 20:46:09.531: W/System.err(17223): at com.example.todolist.DatabaseAdapter.getAllCategory(DatabaseAdapter.java:88)
06-11 20:46:09.531: W/System.err(17223): at com.example.todolist.Categories$1.onClick(Categories.java:40)
06-11 20:46:09.531: W/System.err(17223): at android.view.View.performClick(View.java:3558)
06-11 20:46:09.531: W/System.err(17223): at android.view.View$PerformClick.run(View.java:14152)
06-11 20:46:09.531: W/System.err(17223): at android.os.Handler.handleCallback(Handler.java:605)
06-11 20:46:09.531: W/System.err(17223): at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 20:46:09.531: W/System.err(17223): at android.os.Looper.loop(Looper.java:137)
06-11 20:46:09.531: W/System.err(17223): at android.app.ActivityThread.main(ActivityThread.java:4514)
06-11 20:46:09.531: W/System.err(17223): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 20:46:09.531: W/System.err(17223): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 20:46:09.531: W/System.err(17223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-11 20:46:09.531: W/System.err(17223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-11 20:46:09.531: W/System.err(17223): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 105
Reputation: 881
public List<CategoryClass> getAllCategory()
{
List<CategoryClass> getCategoryList = new ArrayList<CategoryClass>();
dataAdapter = dataAdapter.openToWrite();
Cursor cursor = db.rawQuery("SELECT * FROM " + tb_Name,null);
if(cursor!=null){
cursor.moveToFirst();
while(!cursor.isAfterLast()){
CategoryClass categoryInstance = new CategoryClass();
categoryInstance.setCategory(cursor.getString(1));
// Adding contact to list
getCategoryList.add(categoryInstance);
cursor.moveToNext();
}
}
Log.d("retrieving all categories","count < 0");
return getCategoryList;
}
Upvotes: 1
Reputation: 881
please post your logcat errors... And i think you dont need foreach loop there you can directly pass list to adapter..
DatabaseAdapter dataAdapterInstance;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_list);
ListView list = (ListView) findViewById(R.id.listView);
dataAdapterInstance = new DatabaseAdapter(this);
dataAdapterInstance = dataAdapterInstance.openToWrite();
List<CategoryClass> category = dataAdapterInstance.getAllCategory();
ArrayAdapter<CategoryClass> arrayAdapter = new ArrayAdapter<CategoryClass>(this,android.R.layout.simple_list_item_1,category);
arrayAdapter.notifyDataSetChanged();
list.setAdapter(arrayAdapter);}
Upvotes: 0