从sqlite db实现自定义列表视图适配器的问题

时间:2014-02-17 15:09:54

标签: android sqlite listview

在实现自定义ListView适配器时出现问题,希望有人能够发现我的错误。

在我的活动中,我使用以下方法填充ListView

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);

}

timedata类如下

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;
}


}

在我的databasehandler中,我有以下

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;
}

最后是我的习惯性的

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;
}


}

如果我将上面的textviews设置为静态变量,它就可以工作。但是当设置如上所述时,它会在logcat

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

1 个答案:

答案 0 :(得分:1)

更改setText行; 因为你setinteger值和Android搜索“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()+"");