Reputation: 126
I am trying to display the Map
values in ListView using BaseAdapter. Using the below code I am able to successfully print all the values in Map
. But i don't know how to display a single value of Map
using its key.
MyContactAdapter2.java
public class MyContactAdapter2 extends BaseAdapter {
List<EffectList> contacts;
Context context;
private LayoutInflater mInflater;
// Constructors
public MyContactAdapter2(Context context, List<Map<String, List<EffectList>>> objects) {
this.context = context;
this.mInflater = LayoutInflater.from(context);
this.contacts = new ArrayList<>();
Map<String, List<EffectList>> map = objects.get(0);
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
this.contacts.addAll(map.get(iterator.next()));
}
}
@Override
public int getCount() {
int count = contacts.size();
return count;
}
@Override
public EffectList getItem(int position) {
return contacts.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyContactAdapter2.ViewHolder vh;
if (convertView == null) {
View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
vh = MyContactAdapter2.ViewHolder.create((RelativeLayout) view);
view.setTag(vh);
} else {
vh = (MyContactAdapter2.ViewHolder) convertView.getTag();
}
EffectList item = getItem(position);
vh.textViewName.setText(item.getEffectsId());
vh.textViewEmail.setText(item.getEffectsName());
return vh.rootView;
}
private static class ViewHolder {
public final RelativeLayout rootView;
public final ImageView imageView;
public final TextView textViewName;
public final TextView textViewEmail;
private ViewHolder(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
this.rootView = rootView;
this.imageView = imageView;
this.textViewName = textViewName;
this.textViewEmail = textViewEmail;
}
public static MyContactAdapter2.ViewHolder create(RelativeLayout rootView) {
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
return new MyContactAdapter2.ViewHolder(rootView, imageView, textViewName, textViewEmail);
}
}
}
This is the Json I need to parse,
{
"effect_list": [{
"1":[
{
"effects_id":"1",
"effects_name":"Band 1"
},
{
"effects_id":"2",
"effects_name":"Band 2"
}
],
"2": [
{
"effects_id":"4",
"effects_name":"Background Blur"
},
{
"effects_id":"5",
"effects_name":"Blemish Removal"
}
]
}]
}
I want to display only the values of "1" ("effects_id":"1","effects_name":"Band 1" and "effects_id":"2","effects_name":"Band 2"). How can I achieve it?
I am confused with the code inside constructor too. If you can,Please explain the following code,
this.contacts = new ArrayList<>();
Map<String, List<EffectList>> map = objects.get(0);
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
this.contacts.addAll(map.get(iterator.next()));
EDIT
EffectList.java
public class EffectList {
@SerializedName("effects_id")
@Expose
private String effectsId;
@SerializedName("effects_name")
@Expose
private String effectsName;
public String getEffectsId() {
return effectsId;
}
/* public void setEffectsId(String effectsId) {
this.effectsId = effectsId;
}
*/
public String getEffectsName() {
return effectsName;
}
/*
public void setEffectsName(String effectsName) {
this.effectsName = effectsName;
}
*/
}
Upvotes: 0
Views: 864
Reputation: 720
You are getting objects as List<Map<String, List<EffectList>>>
therefore you are doing a get on the first index to get the first map in the list.
Afterwards you are retrieving the key set of your map, this is a Set which contains all keys that are available in your map.
The iterator is used to retrieve the corresponding List from your map to each key that your map contains.
You could optimize this a bit by doing following:
Set<List<EffectList>> values = map.values;
Iterator<List<EffectList>> it = values.iterator();
while(it.hasNext() {
this.contacts.addAll(it.next());
}
To archive the desired output you could filter the EffectList before adding this to the contacts list.
EDIT
As example(using google commons collections):
List<EffectList> itValue = it.next();
List<EffectList> filteredValue = Collections.filter(itValue, new Predicate<EffectList>(){
public boolean apply(EffectList input){
if(input.getEffectsId().equals("1") || input.getEffectsId().equals("2"){
return true;
}
return false;
}
);
this.contacts.addAll(filteredValue);
Upvotes: 1