Reputation: 45
Is there a way to show only a specific element from ArrayList using an ArrayAdapter
final ListView lst = (ListView) findViewById(R.id.listView);
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
for(int j=0;j<result.length();j++) {
try {
HashMap<String,String> map = new HashMap<String,String>();
map.put("title", result.getJSONObject(j).getString("title"));
map.put("url", result.getJSONObject(j).getString("url"));
list.add(map);
} catch (Exception e) {
e.printStackTrace();
}
}
ArrayAdapter ad= new ArrayAdapter(List.this, android.R.layout.simple_list_item_1,list);
lst.setAdapter(ad);
in other words: I want to show only title element in a ListView.
Do I need to create a CustomAdapter?
Upvotes: 2
Views: 101
Reputation: 1066
if you need String.
String title=(String)map.get("title");
if you need object.
Object title=map.get("title");
Hope it works. Thanks
Upvotes: 0
Reputation: 6356
Yes, you have to create a custom adapter in this case or you can simple create an additional list and use it for ArrayAdapter.
You can also create extended List for you purposes and pass HashMap into it
Upvotes: 0
Reputation: 157447
you can use a SimpleAdapter. E.g.
SimpleAdapter ad= new SimpleAdapter(List.this, list, android.R.layout.simple_list_item_1, new String[] {"title"}, new int[] {android.R.id.textview1});
the String[] contains the key you want to use to retrieve the values to set in your ui
Upvotes: 1
Reputation: 35559
There are 2 ways to do it :
By creating another arraylist with just value you want to show. like below :
ArrayList<String> listTitle = new ArrayList<String>();
for(int j=0;j<result.length();j++) {
try {
HashMap<String,String> map = new HashMap<String,String>();
map.put("title", result.getJSONObject(j).getString("title"));
map.put("url", result.getJSONObject(j).getString("url"));
listTitle.add(result.getJSONObject(j).getString("title"));
list.add(map);
} catch (Exception e) {
e.printStackTrace();
}
}
ArrayAdapter ad= new ArrayAdapter(List.this, android.R.layout.simple_list_item_1,listTitle);
Upvotes: 0