2014-02-17 90 views
0

執行自定義ListView適配器時出現問題,希望有人能夠發現我的錯誤。從sqlite db執行自定義列表視圖適配器的問題

在我的活動我填充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; 
} 


} 

在我的數據庫處理器我有以下

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

而且終於我的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; 
} 


} 

如果我將上面的textviews設置爲靜態變量,它將起作用。但是,當設置爲高於其同在logcat的

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

回答

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()+""); 
+0

這樣做!謝謝 – Gary

相關問題