Gary
Gary

Reputation: 513

Problems implementing custom listview adapter from sqlite db

Having problems implementing a custom ListView adapter, hoping someone can spot my error.

In my activity I populate the ListView with this method:

private void populateListView() {

    lv = (ListView) findViewById(R.id.listViewSetTimes);

    DatabaseHandler db = new DatabaseHandler(getApplicationContext());

    ArrayList<TimeData> timedata = new ArrayList<TimeData>(); 
    timedata = db.getTimeData();


    CustomListAdapter customListAdapter = new CustomListAdapter(AddtimeActivity.this, timedata);

    lv.setAdapter(customListAdapter); 
    registerForContextMenu(lv);

}

The timedata class is as follows

package library; 

public class TimeData {

//private variables

private int start_hr; 
private int start_min; 
private int stop_hr;
private int stop_min;



// getting start hour
public int getStart_hr(){
    return this.start_hr;
}

// setting start hour
public void setStart_hr(int start_hr){
    this.start_hr = start_hr;
}

// getting start min
public int getStart_min(){
    return this.start_min;
}

// setting start min
public void setStart_min(int start_min){
    this.start_min = start_min;
}

// getting stop hour
public int getStop_hr(){
    return this.stop_hr;
}

// setting start hour
public void setStop_hr(int stop_hr){
    this.stop_hr = stop_hr;
}
// getting stop min
public int getStop_min(){
    return this.stop_min;
}

// setting start min
public void setStop_min(int stop_min){
    this.stop_min = stop_min;
}


}

Inside my databasehandler I have the following

public ArrayList<TimeData> getTimeData() {
        ArrayList<TimeData> TimeDataList = new ArrayList<TimeData>();
        LockTimeList.clear();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_TIMES;

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

    // looping through all rows and adding to list
    if (cursor != null && cursor.getCount() != 0) {
    if (cursor.moveToFirst()) {
        do {
            TimeData timedata = new TimeData();

            timedata .setStart_hr(cursor.getInt(2));
            timedata .setStart_min(cursor.getInt(3));
            timedata .setStop_hr(cursor.getInt(4));
            timedata .setStop_min(cursor.getInt(5));

            // Adding data to list
            TimeDataList.add(timedata );
        } while (cursor.moveToNext());
    }
    }
    cursor.close();

    // return contact list
    return TimeDataList;
}

And finally my customadapter

public class CustomListAdapter extends BaseAdapter {


Context context;
ArrayList<TimeData> timedata;

public CustomLockAdapter(Context context, ArrayList<TimeData> list) {

    this.context = context;
    timedata = list;
}

@Override
public int getCount() {

    return lockdata.size();
}

@Override
public Object getItem(int position) {

    return lockdata.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    TimeData timedatalistitems = timedata.get(position);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.time_list_row, null);

    }
    TextView textAlarmStart = (TextView) convertView.findViewById(R.id.textAlarmStart);
    textAlarmStart.setText(timedatalistitems .getStart_hr());
    TextView textAlarmStop = (TextView) convertView.findViewById(R.id.textAlarmStop);
    textAlarmStop.setText(timedatalistitems .getStop_hr());

    return convertView;
}


}

If I set the textviews above to static variables it works. But when set as above its crashing with the following in the logcat

02-17 09:35:04.280: E/AndroidRuntime(1400): android.content.res.Resources$NotFoundException: String resource ID #0x8

Upvotes: 0

Views: 68

Answers (1)

nurisezgin
nurisezgin

Reputation: 1570

Change your setText lines; because you setinteger value and Android search "R.string."

TextView textAlarmStart = (TextView) convertView.findViewById(R.id.textAlarmStart);
textAlarmStart.setText(timedatalistitems .getStart_hr()+"");
TextView textAlarmStop = (TextView) convertView.findViewById(R.id.textAlarmStop);
textAlarmStop.setText(timedatalistitems .getStop_hr()+"");

Upvotes: 1

Related Questions