Страница 1 из 1

SimpleCursorAdapter проблемы с заполнением ListView

Добавлено: 20 мар 2014, 09:23
Eserchik
Помогите решить задачу. У меня ListView начинает заполняться данными, только после первой прокрутки списка. Т.е в начальный момент список пустой.

Проблемма скореее всего в этом блоке if/else:

Код: Выделить всё

if (inView == null) {
  }else {

}
Вот мой адаптер:

Код: Выделить всё

public class MyDataAdapter extends SimpleCursorAdapter{
private Cursor cursor;
private Context context;
private ArrayList<String> list = new ArrayList<String>();
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
private LayoutInflater inflater;
private ViewHolder holder;
private String rowID;
// itemChecked will store the position of the checked items.

public MyDataAdapter(Context context, int layout, Cursor c, String[] from,
        int[] to) {
    super(context, layout, c, from, to);
    this.cursor = c;
    this.context = context;

    for (int i = 0; i < this.getCount(); i++) {
        itemChecked.add(i, false); // initializes all items value with false
    }
}

class ViewHolder {

    public CheckBox cBox;
}

public View getView(final int pos, View inView, ViewGroup parent) {
    if (inView == null) {
       inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inView = inflater.inflate(R.layout.row_list_view, null);
        holder = new ViewHolder();
        holder.cBox = (CheckBox) inView.findViewById(R.id.chbox1); // your

        inView.setTag(holder);
    }else {

         holder = (ViewHolder) inView.getTag();
         bindView(inView, context, cursor);

    }

    // CheckBox
    holder.cBox.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            CheckBox cb = (CheckBox) v.findViewById(R.id.chbox1);

            if (cb.isChecked()) {
                itemChecked.set(pos, true);
                Toast.makeText(context, "true", Toast.LENGTH_LONG).show();
                // do some operations here
            } else if (!cb.isChecked()) {
                itemChecked.set(pos, false);
                // do some operations here
                Toast.makeText(context, "false", Toast.LENGTH_LONG).show();
            }
        }
    });
    holder.cBox.setChecked(itemChecked.get(pos)); // this will Check or Uncheck the

    return inView;
}

}