#android
#Android
Вопрос:
Я использую следующий пользовательский адаптер для заполнения моего элемента управления списком из курсора DB. Я не могу понять, почему этот код выходит из строя в конструкторе именно при вызове super.
public class ListAdaptor extends SimpleCursorAdapter {
private Cursor dataCursor;
private LayoutInflater mInflater;
class ViewHolder {
public TextView label = null;
public CheckBox chkBx = null;
public TextView price = null;
public TextView weight = null;
}
//constructor
public ListAdaptor(Context context, int layout, Cursor dataCursor, String[] from, int[] to) {
super(context, layout, dataCursor, from, to);
this.dataCursor = dataCursor;
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
// Inflate the view
convertView = mInflater.inflate(R.layout.listviewlyt, null);
// Get the ID's of the views
TextView tmpLbl = (TextView)convertView.findViewById(R.id.label);
CheckBox tmpChkBx = (CheckBox)convertView.findViewById(R.id.chkbox);
TextView tmpPrc = (TextView)convertView.findViewById(R.id.labelPrice);
TextView tmpWt = (TextView)convertView.findViewById(R.id.labelWt);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.label = tmpLbl;
holder.chkBx = tmpChkBx;
holder.price = tmpPrc;
holder.weight = tmpWt;
// Set the Tag
convertView.setTag(holder);
}
else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
// Cursor to current item
dataCursor.moveToPosition(position);
String keyWrd = dataCursor.getString(2);
String price = dataCursor.getString(3);
TextView labelRef = holder.label;
CheckBox chbxRef = holder.chkBx;
TextView labelPrc = holder.price;
TextView labelWt = holder.weight;
labelRef.setText(keyWrd);
labelPrc.setText(price);
//chbxRef.setChecked(refObj.flag);
//labelWt.setText(refObj.wt);
return convertView;
}
}
Кто-нибудь может помочь мне найти причину?
Комментарии:
1. Поток [<3> основной] (приостановлен) ActivityThread.performLaunchActivity (ActivityThread$ActivityRecord, намерение) строка: 2494 ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, намерение) строка: 2512 ActivityThread.access $2200 (ActivityThread, ActivityThread$ActivityRecord, намерение) строка: 119 ActivityThread $H.Строка handleMessage (сообщение): 1863 ActivityThread$H (Обработчик).DispatchMessage (сообщение) строка: 99 Looper.loop() строка: 123 ActivityThread.main(строка[]) строка: 4363
Ответ №1:
Скорее всего, эта строка может вызывать проблемы
mInflater = LayoutInflater.from(context);
Комментарии:
1. Спасибо, Rajdeep. Я понял это. Проблема заключалась в том, что я не использовал _Id в своей базе данных, из-за чего курсор не работал. Кроме того, я обнаружил, что проще расширить класс адаптера из CusrorAdaptor и переопределить функции-члены BindView и NewView.