Reputation: 115
In class 1 I have a hashmap which I send to my 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));
But in class 2 my MyCustomAdapter getView
function is not getting called, can you help understand why ?
thanks
CODE
//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
Upvotes: 0
Views: 3627
Reputation: 11
Must declare Size.
Your GetCount()
function must declare size... Set the return to 1 and your list will be populated by one item.
So if you are passing in a cursor, just return cursor.getCount();
Soo... And you List will be populated Properly.
@Override
public int getCount() {
return cursor.getCount();
}
In your case return the size of your map.
Upvotes: 1
Reputation: 42016
getCount()
How many items are in the data set represented by this Adapter. http://developer.android.com/reference/android/widget/Adapter.html
@Override
public int getCount() {
// TODO Auto-generated method stub
return size;// more than zero
}
getItem()
must return something
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return my_VALUES;// may be in your case
}
Upvotes: 4