ListView doesn't appear in ListActivity
my code is a ListActvity to Load data from dabase to 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)开发者_JAVA技巧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;
}
}
I see in Log cat the method public View getView(int position, View convertView, ViewGroup parent) doen't invoked and listviewitems doesn't appear on listview how to fix it? Thank you for support.
Don't roll your own adapter when you can use 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));
}
You return 0 inside the adapters getCount()
method. This results in a ListView
with no entries, which just doesn't get displayed.
getItemId()
are also returnins always the same value (0), which is not correct to get a working adapter. You can just return the position
argument to fix this.
精彩评论