ListView没有出现在ListActivity中

时间:2011-09-16 16:38:45

标签: android listadapter

我的代码是一个ListActvity,用于将数据从dabase加载到listview

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

}

我在Log cat中看到方法public View getView(int position,View convertView,ViewGroup parent)没有调用,listviewitems没有出现在listview上 怎么解决? 感谢您的支持。

2 个答案:

答案 0 :(得分:1)

当您可以使用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));
}

答案 1 :(得分:0)

在适配器getCount()方法中返回0。这导致ListView没有条目,只是没有显示。

getItemId()也是returnins总是相同的值(0),这对于获得工作适配器是不正确的。您只需返回position参数即可解决此问题。