2013-07-03 62 views
0

我的活動課getView()方法不調用自定義的ListView的Android

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方法。什麼是我無法識別的問題。我的代碼有什麼問題。任何人都可以幫助我,非常感謝。

回答

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(); 
} 
相關問題