getView()方法不调用自定义listView Android

时间:2013-07-03 13:16:24

标签: android listview cursor adapter

我的活动类

public class BRActivityList extends Activity {
private UserDBAdapter dbHelper;
ListView list;
MyListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bractivitylist);
    dbHelper=new UserDBAdapter(this);
    displayBRActivityList();
}
private void displayBRActivityList()
{
    ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
    dbHelper.open();
    Cursor cursor=dbHelper.fetchAllData();
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(LoginDatabaseOpenHelper.COLUMN_CONSUMERNAME, cursor.getString(cursor.getColumnIndex(LoginDatabaseOpenHelper.COLUMN_CONSUMERNAME)));
        map.put(LoginDatabaseOpenHelper.COLUMN_AGE, cursor.getString(cursor.getColumnIndex(LoginDatabaseOpenHelper.COLUMN_AGE)));
        map.put(LoginDatabaseOpenHelper.COLUMN_MOBILE, cursor.getString(cursor.getColumnIndex(LoginDatabaseOpenHelper.COLUMN_MOBILE)));
        map.put(LoginDatabaseOpenHelper.COLUMN_DIST, cursor.getString(cursor.getColumnIndex(LoginDatabaseOpenHelper.COLUMN_DIST)));
        map.put(LoginDatabaseOpenHelper.COLUMN_BRAND, cursor.getString(cursor.getColumnIndex(LoginDatabaseOpenHelper.COLUMN_BRAND)));
        map.put(LoginDatabaseOpenHelper.COLUMN_DOB, cursor.getString(cursor.getColumnIndex(LoginDatabaseOpenHelper.COLUMN_DOB)));
        map.put(LoginDatabaseOpenHelper.COLUMN_AGRIMG, cursor.getString(cursor.getColumnIndex(LoginDatabaseOpenHelper.COLUMN_AGRIMG)));
        map.put(LoginDatabaseOpenHelper.COLUMN_SIGNATURE, cursor.getString(cursor.getColumnIndex(LoginDatabaseOpenHelper.COLUMN_SIGNATURE)));
        dataList.add(map);
      cursor.moveToNext();
    }
 // Make sure to close the cursor
    cursor.close();
    dbHelper.close();
    list=(ListView)findViewById(R.id.list);     
    list.setAdapter(new MyListAdapter(this, dataList));
}

}

我的适配器类

public class MyListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
private AlbumStorageDirFactory mAlbumStorageDirFactory = null;

public MyListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
}


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


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


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


public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView consumername = (TextView)vi.findViewById(R.id.consumername); // title
    TextView consumerage = (TextView)vi.findViewById(R.id.consumerage); // artist name
    TextView consumermob = (TextView)vi.findViewById(R.id.consumermob); // duration
    TextView consumerdist=(TextView)vi.findViewById(R.id.consumerdist);
    TextView consumerdob=(TextView)vi.findViewById(R.id.consumerdob);
    TextView brand=(TextView)vi.findViewById(R.id.brand);
    ImageView consumerimg=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> consumerdata = new HashMap<String, String>();
    consumerdata = data.get(position);

    // Setting all values in listview
    consumername.setText(consumerdata.get(LoginDatabaseOpenHelper.COLUMN_CONSUMERNAME));
    consumerage.setText(consumerdata.get(LoginDatabaseOpenHelper.COLUMN_AGE));
    consumermob.setText(consumerdata.get(LoginDatabaseOpenHelper.COLUMN_MOBILE));
    consumerdist.setText(consumerdata.get(LoginDatabaseOpenHelper.COLUMN_DIST));
    consumerdob.setText(consumerdata.get(LoginDatabaseOpenHelper.COLUMN_DOB));
    brand.setText(consumerdata.get(LoginDatabaseOpenHelper.COLUMN_BRAND));
    Resources res = activity.getResources();
    consumerimg.setImageDrawable(res.getDrawable(R.drawable.rihanna));
    return vi;
}
}

我调试了好几次但它从不调用getView方法。我无法识别出什么问题。我的代码出了什么问题。非常感谢任何人帮助我。

1 个答案:

答案 0 :(得分:4)

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

getCount()必须返回数据集中的条目数。改变它:

public int getCount() {
    // TODO Auto-generated method stub
    return (data == null) ? 0 : data.size();
}