Reputation: 89
I've been struggling with populating a ListView in android for a while. Can someone please give me a hand here?
So, I have saved data in a database in 2 different tables. One for settings and one for data to be displayed in a listView. But after watching quite a bunch of tutorials I keep having trouble with populating the list. I am giving you some code, and I hope you guys can help me solve the issue.
The MainActivity:
public class MainActivity extends AppCompatActivity {
//private ArrayList<String> arrayList;
public ListView viewEvents;
DataHandler dataBase;
private ListView listView;
// SQLiteDatabase data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.ElementsView);
dataBase = new DataHandler(this);
showList();
}
public void showList() {
try{
ArrayList<ListDisplayItems> displayList = new ArrayList<ListDisplayItems>();
//dataBase.open();
displayList.clear();
//String query = "SELECT * FROM " + DataHandler.DATABASE_TABLE_DISPLAY;
Cursor c1 = DataHandler.displayQuery();
// Cursor c1 = data.rawQuery(query, null);
if (c1 != null && c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
ListDisplayItems listDisplayItems = new ListDisplayItems();
listDisplayItems.SetTime(c1.getString(c1
.getColumnIndex(DataHandler.KEY_TIME_TEXT)));
listDisplayItems.SetName(c1.getString(c1
.getColumnIndex(DataHandler.KEY_NAME)));
listDisplayItems.SetDays(c1.getString(c1
.getColumnIndex(DataHandler.KEY_DAYS_OF_WEEK_TEXT)));
displayList.add(listDisplayItems);
} while (c1.moveToNext());
}
}
c1.close();
//dataBase.close();
DisplayListAdapter displayListAdapter = new DisplayListAdapter(
MainActivity.this, displayList);
listView.setAdapter(displayListAdapter);
}catch(Exception e){
e.printStackTrace();
}
}
The DisplayListAdapter Class:
DataHandler database;
Context context;
ArrayList<ListDisplayItems> displayList;
public DisplayListAdapter(Context context, ArrayList<ListDisplayItems> list) {
this.context = context;
displayList = list;
}
@Override
public int getCount() {
return displayList.size();
}
@Override
public Object getItem(int position) {
return displayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ListDisplayItems displayItemsList = displayList.get(position);
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_layout, null);
}
TextView textTime = (TextView) convertView.findViewById(R.id.itextTime);
textTime.setText(displayItemsList.GetTime());
TextView textName = (TextView) convertView.findViewById(R.id.itextName);
textName.setText(displayItemsList.GetName());
TextView textDays = (TextView) convertView.findViewById(R.id.itextDays);
textDays.setText(displayItemsList.GetDays());
return convertView;
}
}
The ListDisplayItems Class:
public class ListDisplayItems {
String iName;
String iIconRing;
String iIconAlarm;
String iTimeText;
String iDaysOfWeek;
public String GetName()
{
return iName;
}
public void SetName(String Name)
{
this.iName = Name;
}
public String GetIconRing()
{
return iIconRing;
}
public void SetIconRing(String Icon)
{
this.iIconRing = Icon;
}
public String GetIconAlarm()
{
return iIconAlarm;
}
public void SetIconAlarm(String Icon)
{
this.iIconAlarm = Icon;
}
public String GetTime()
{
return iTimeText;
}
public void SetTime(String Time)
{
this.iTimeText = Time;
}
public String GetDays()
{
return iDaysOfWeek;
}
public void SetDays(String Days)
{
this.iDaysOfWeek = Days;
}
}
And my DataHandler Class:
public class DataHandler{
public static final String KEY_EXAMPLE = "example_of_my_keys";
static DbHelper dbhelper;
Context ctx;
static SQLiteDatabase dataBase;
public DataHandler(Context ctx){
this.ctx = ctx;
dbhelper = new DbHelper(ctx);
}
public static Cursor displayQuery() {
Cursor c1 = null;
try {
if (dataBase.isOpen()) {
dataBase.close();
}
String[] projection = {
KEY_NAME,
KEY_TIME_TEXT,
KEY_DAYS_OF_WEEK_TEXT,
};
String sortOrder =
KEY_ROWID + " ASC";
dataBase = dbhelper.getWritableDatabase();
//c1 = dataBase.rawQuery(query, null);
c1 = dataBase.query(
DATABASE_TABLE_DISPLAY, // The table to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
return c1;
}
private class DbHelper extends SQLiteOpenHelper{
.....code creating tables and stuff///
}
}
public DataHandler open()
{
dataBase = dbhelper.getWritableDatabase();
return this;
}
public void close()
{
dbhelper.close();
}
}
I have created a custom view layout for the listView, with icons set. No errors or anything. I only get a handled NullPointer exception and nothing appears on my list.
Here is my log:
01-30 14:27:08.641: E/Trace(656): error opening trace file: No such file or directory (2) 01-30 14:27:09.419: W/dalvikvm(656): VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 01-30 14:27:09.419: I/dalvikvm(656): Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 01-30 14:27:09.429: W/dalvikvm(656): VFY: unable to resolve interface method 14535: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 01-30 14:27:09.429: D/dalvikvm(656): VFY: replacing opcode 0x72 at 0x0002 01-30 14:27:09.439: I/dalvikvm(656): Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 01-30 14:27:09.459: W/dalvikvm(656): VFY: unable to resolve interface method 14539: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 01-30 14:27:09.459: D/dalvikvm(656): VFY: replacing opcode 0x72 at 0x0002 01-30 14:27:09.989: I/dalvikvm(656): Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged 01-30 14:27:09.989: W/dalvikvm(656): VFY: unable to resolve virtual method 14435: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V 01-30 14:27:09.989: D/dalvikvm(656): VFY: replacing opcode 0x6f at 0x0007 01-30 14:27:10.069: I/dalvikvm(656): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 01-30 14:27:10.069: W/dalvikvm(656): VFY: unable to resolve virtual method 418: Landroid/content/res/TypedArray;.getChangingConfigurations ()I 01-30 14:27:10.079: D/dalvikvm(656): VFY: replacing opcode 0x6e at 0x0002 01-30 14:27:10.109: I/dalvikvm(656): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 01-30 14:27:10.109: W/dalvikvm(656): VFY: unable to resolve virtual method 440: Landroid/content/res/TypedArray;.getType (I)I 01-30 14:27:10.129: D/dalvikvm(656): VFY: replacing opcode 0x6e at 0x0002 01-30 14:27:10.429: I/System.out(656): DATABASE ERROR java.lang.NullPointerException 01-30 14:27:10.429: W/System.err(656): java.lang.NullPointerException 01-30 14:27:10.439: W/System.err(656): at com.teosoft.mutetimer.MainActivity.showList(MainActivity.java:55) 01-30 14:27:10.439: W/System.err(656): at com.teosoft.mutetimer.MainActivity.onCreate(MainActivity.java:27) 01-30 14:27:10.449: W/System.err(656): at android.app.Activity.performCreate(Activity.java:5008) 01-30 14:27:10.449: W/System.err(656): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 01-30 14:27:10.479: W/System.err(656): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 01-30 14:27:10.479: W/System.err(656): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 01-30 14:27:10.499: W/System.err(656): at android.app.ActivityThread.access$600(ActivityThread.java:130) 01-30 14:27:10.540: W/System.err(656): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 01-30 14:27:10.549: W/System.err(656): at android.os.Handler.dispatchMessage(Handler.java:99) 01-30 14:27:10.559: W/System.err(656): at android.os.Looper.loop(Looper.java:137) 01-30 14:27:10.559: W/System.err(656): at android.app.ActivityThread.main(ActivityThread.java:4745) 01-30 14:27:10.589: W/System.err(656): at java.lang.reflect.Method.invokeNative(Native Method) 01-30 14:27:10.589: W/System.err(656): at java.lang.reflect.Method.invoke(Method.java:511) 01-30 14:27:10.589: W/System.err(656): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 01-30 14:27:10.609: W/System.err(656): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 01-30 14:27:10.609: W/System.err(656): at dalvik.system.NativeStart.main(Native Method) 01-30 14:27:10.889: D/dalvikvm(656): GC_CONCURRENT freed 197K, 3% free 11034K/11335K, paused 115ms+53ms, total 392ms 01-30 14:27:11.949: I/Choreographer(656): Skipped 109 frames! The application may be doing too much work on its main thread. 01-30 14:27:12.029: D/gralloc_goldfish(656): Emulator without GPU emulation detected.
Sorry for the long post. I'm a newbie. :)
Upvotes: 0
Views: 366
Reputation: 1583
static SQLiteDatabase dataBase; // this field is not initialized => dataBase = null
public DataHandler(Context ctx){
this.ctx = ctx;
dbhelper = new DbHelper(ctx);
}
public static Cursor displayQuery() {
Cursor c1 = null;
try {
if (dataBase.isOpen()) { // You try to use dataBase here and it is null
dataBase.close();
}
...
Replace that if block with
if (dataBase != null && dataBase.isOpen()) {
dataBase.close();
}
This should work.
Upvotes: 1
Reputation: 1583
I think the problem is at this line:
c1.close();
c1 seems to be null and you try to close it even if it is null
if (c1 != null && c1.getCount() != 0) {
// code
}
c1.close();
Move that line inside if block and the app shouldn't crash. But the list will be empty.
Upvotes: 0