ListView не отображается в ListActivity

мой код представляет собой ListActivity для загрузки данных из dabase в список

public class ViewList extends ListActivity {
private ListViewAdapter lAdapter;   

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    lAdapter = new ListViewAdapter();
    DBAdapter db = new DBAdapter(this);
    db.open();      
    Cursor cursor = db.fetchAllDeliveryItem();
    lAdapter.setCursor(cursor);
    cursor.moveToFirst();
    //Toast.makeText(getApplicationContext(), cursor.getString(1), Toast.LENGTH_SHORT).show();
    setListAdapter(lAdapter);

}
private class ListViewAdapter extends BaseAdapter{
    //String data [] = {"_id","itemname","pickupaddress","deliveryaddress","delivered"};
    private LayoutInflater mInflater;
    //private ArrayList mData = new ArrayList();
    private Cursor cursor;  
     public ListViewAdapter() {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

    public void setCursor(Cursor cursor)
    {
        this.cursor = cursor;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Log.i("GetView RUN","Runing runing");
        if (convertView == null) 
        {
            convertView = mInflater.inflate(R.layout.receiverow, null);
            holder = new ViewHolder();
            holder.textView = (TextView)convertView.findViewById(R.id.label);
            holder.checkbox = (CheckBox)convertView.findViewById(R.id.check);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }           
       // String getPosition[] = (String) mData.get(position);
        //holder.textView.setText(mData.get(position));    
       //holder.textView.setText(cursor.getString(1));
        holder.textView.setText("Test");
       if(cursor.getInt(4)!=0)
       holder.checkbox.setChecked(true);
       else
           holder.checkbox.setChecked(false);
        return convertView;
    }

}
public static class ViewHolder {
    public TextView textView;
    public CheckBox checkbox;
}

}

Я вижу в журнале cat, что метод public View getView(int position, View convertView, ViewGroup parent) не вызывается, а listviewitems не отображаются в списке, как это исправить? Спасибо за поддержку.


person Professo Bean    schedule 16.09.2011    source источник
comment
Почему вы переопределяете getView?   -  person Dan S    schedule 16.09.2011


Ответы (2)


Не сворачивайте собственный адаптер, если вы можете использовать SimpleCursorAdapter:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    DBAdapter db = new DBAdapter(this);
    db.open();
    Cursor cursor = db.fetchAllDeliveryItem();

    // *** replace these strings with the actual *column names* of your query!
    String[] from = { "name_label_column", "name_check_column" };
    int[] to = { R.id.label, R.id.check };

    setListAdapter(new SimpleCursorAdapter(this, R.layout.receiverow, cursor, from, to));
}
person Philipp Reichart    schedule 16.09.2011

Вы возвращаете 0 внутри метода адаптера getCount(). Это приводит к ListView без записей, которые просто не отображаются.

getItemId() также всегда возвращают одно и то же значение (0), что неправильно для получения работающего адаптера. Вы можете просто вернуть аргумент position, чтобы исправить это.

person Community    schedule 16.09.2011