在第 1 类中,我有一个哈希图,我将它发送到我的 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 类中,我的 MyCustomAdaptergetView
函数没有被调用,你能帮助理解为什么吗?
谢谢
代码
//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