在第1课中,我有一个hashmap,我发送到我的CustomAdapter。
map.put("year", "Apple");
map.put("make", "Mango");
map.put("model", "Grape");
map.put("style", "Orange");
map.put("series", "Peach");
//link to my adapter
setListAdapter(new MyCustomAdapter(DynamicLists.this, R.layout.row, map));
但是在第2课我的MyCustomAdapter getView
函数没有被调用,你能帮忙理解为什么吗?
感谢
代码
//class 1
public class DynamicLists extends ListActivity {
//class 2
public class MyCustomAdapter extends BaseAdapter {
String my_VALUES;
public MyCustomAdapter(Context context, int textViewResourceId,
HashMap<String, String> map) {
String[][] array = new String[map.size()][2];
int count = 0;
String combined="";
for(Map.Entry<String, String> entry : map.entrySet()){
combined=""+entry.getKey()+""+entry.getValue();
count++;
}
my_VALUES = combined;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
if (row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, null);
}
TextView label =(TextView)row.findViewById(R.id.blocked);
label.setText(my_VALUES);
return row;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
} //end of class 2
private static final String TAG = "Example";
public static HashMap<String, String> map = new HashMap<String, String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//tie data to list, call constructor MyCustomAdapter
//populate the list. ArrayAdapter takes current class, layout and array
map.put("year", "Apple");
map.put("make", "Mango");
map.put("model", "Grape");
map.put("style", "Orange");
map.put("series", "Peach");
//link to my adapter
setListAdapter(new MyCustomAdapter(DynamicLists.this, R.layout.row, map));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//super.onListItemClick(l, v, position, id);
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}
} //end of class 1
答案 0 :(得分:4)
getCount()
此适配器表示的数据集中有多少项。 http://developer.android.com/reference/android/widget/Adapter.html
@Override
public int getCount() {
// TODO Auto-generated method stub
return size;// more than zero
}
getItem()
必须返回
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return my_VALUES;// may be in your case
}
答案 1 :(得分:1)
必须声明尺寸。
您的GetCount()
函数必须声明大小...将返回值设置为1,您的列表将填充一个项目。
因此,如果您传入游标,只需返回cursor.getCount();
Soo ......你的名单将正确填充。
@Override
public int getCount() {
return cursor.getCount();
}
在您的情况下,返回地图的大小。