2017-01-06 2 views
0

BaseAdapterを使用してListViewにMapの値を表示しようとしています。以下のコードを使用すると、すべての値をMapに正常に印刷できます。しかし、私はを使用してMapという単一の値を表示する方法を知らない。ListViewでマップ値を表示する方法は?

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); 
     } 
    } 
} 

これは私が

{ 
    "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" 
      } 
      ] 
     }] 
    } 

が、私は "1" ( "effects_id" の値のみを表示したい、解析する必要がJSONです:」 1 "、" effects_name ":" Band 1 "、" effects_id ":" 2 "、" effects_name ":" Band 2 ")。どうすれば達成できますか?

コンストラクタ内のコードと混同しています。あなたは、次のコード、

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; 
    } 
    */ 
} 
+0

あなたもEffectListのソースコードを提供してもらえますか? – ZeusNet

+0

@ ZeusNet、編集。 – user7357013

答えて

1

を説明してくださいすることができます場合は、したがって、あなたが最初のインデックスに乗る行っているList<Map<String, List<EffectList>>>などのオブジェクトを取得していますリスト内の最初のマップを取得します。 その後、マップのキーセットを再調整します。これは、マップ内で使用可能なすべてのキーを含むセットです。 イテレータは、マップからマップに含まれる各キーに対応するリストを取得するために使用されます。

あなたは少し次のようにして、これを最適化することができます:

Set<List<EffectList>> values = map.values; 
Iterator<List<EffectList>> it = values.iterator(); 
while(it.hasNext(){ 
this.contacts.addAll(it.next()); 
} 

を使用すると、連絡先リストにこれを追加する前にEffectListをフィルタリングすることができ所望の出力をアーカイブします。

EDIT例として

(グーグルコモンズのコレクションを使用して):

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); 
関連する問題