2016-12-23 2 views
0

JSONレスポンスに応じて、cardviewでテキストと画像を設定しようとしています。私はフラグメント、cardadapter、と私は今、私のcardview上の名前やショーを解析できる項目のクラスを使用していますが、今、私は私の無線LANのアイコンを表示したいJSONレスポンスからカードビューに画像を設定

{["name":"Store 1","wifi":true, ...]} 

:私のJSONはこの情報が含まれていますwifiの応答が真であればdrawableファイル、falseの場合はアイコンを表示しません。

私の断片中のデータを解析するためのコード:

private void parseData(JSONArray array){ 

    for(int i = 0; i<array.length(); i++) { 
     StoreListCard storeListCard = new StoreListCard(); 
     JSONObject json = null; 
     try { 
      json = array.getJSONObject(i); 

      //Parse name 
      storeListCard.setName(json.getString(Config.TAG_NAME)); 


      if(json.getBoolean(Config.TAG_WIFI) == true){ 
       storeListCard.setWifi(json.getBoolean(Config.TAG_WIFI)); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     storeListCards.add(storeListCard); 
    } 

} 

StoreListCard.java:

public class StoreListCard { 
    private String name; 
    private Boolean wifi; 

     public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public Boolean getWifi() { 
    return wifi; 
} 

public void setWifi(Boolean wifi) { 
    this.wifi = wifi; 
} 
} 

CardAdapter:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder>{ 

    List<StoreListCard> storeListCards; 
    public CardAdapter(List<StoreListCard> storeListCards, Context context){ 
    super(); 
    //Getting all the information 
    this.storeListCards = storeListCards; 
    this.context = context; 
} 


@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.store_cardview, parent, false); 
    ViewHolder viewHolder = new ViewHolder(v); 
    return viewHolder; 
} 

    @Override 
public void onBindViewHolder(ViewHolder holder, int position) { 

    StoreListCard storeListCard = storeListCards.get(position); 
    holder.tvStoreName.setText(storeListCard.getName()); 

    //This part I am not sure 
    holder.ivWifiIcon.setImageResource(); 
} 

    @Override 
public int getItemCount() { 
    return storeListCards.size(); 
} 

    class ViewHolder extends RecyclerView.ViewHolder{ 
    public TextView tvStoreName; 
    public ImageView ivWifiIcon; 

    public ViewHolder(View itemView) { 
     super(itemView); 

     tvStoreName = (TextView) itemView.findViewById(R.id.tvStoreName); 
     ivWifiIcon = (ImageView) itemView.findViewById(R.id.ivWifiIcon); 

    } 
} 
} 

XML(名前とWIFIする部分):

  <ImageView 
       android:layout_width="20dp" 
       android:layout_height="20dp" 
       android:layout_toRightOf="@id/ivWaitingIcon" 
       android:background="@drawable/tool_wifi_icon_02" 
       android:layout_marginLeft="5dp" 
       android:layout_alignBottom="@id/ivWaitingIcon" 
       android:id="@+id/ivWifiIcon"/> 

      <TextView 
       android:id="@+id/tvStoreName" 
       android:textStyle="bold" 
       android:paddingRight="10dp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="20dp" 
       android:layout_gravity="left" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:textColor="#000000"/> 

私は今、成功し表示する名前を取得することができますが、私は、これを試してみてください。この

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 

    StoreListCard storeListCard = storeListCards.get(position); 
    holder.tvStoreName.setText(storeListCard.getName()); 

     if (storeListCard.getWifi()){ 
      holder.ivWifiIcon.setImageResource(R.drawable.your_wifie_icon); 
     } else { 
     holder.ivWifiIcon.setImageResource(R.drawable.any_place_holder_icon); 
     } 
} 
+0

@IntelliJAmiyaいいえ、画像は私のドロウアブルフォルダからです.Jsonは取得した画像が真または偽であることを示します。そして、応答が私に真実を与えるなら、私はdrawableフォルダから画像を表示します – JerryKo

答えて

1

インサイドonBindViewHolderに応じたアイコンを表示するかどうかはわからない

private void parseData(JSONArray array){ 

    for(int i = 0; i<array.length(); i++) { 
     StoreListCard storeListCard = new StoreListCard(); 
     JSONObject json = null; 
     try { 
      json = array.getJSONObject(i); 

      //Parse name 
      storeListCard.setName(json.getString(Config.TAG_NAME)); 
      storeListCard.setWifi(json.getBoolean(Config.TAG_WIFI)); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     storeListCards.add(storeListCard); 
    } 

} 

カードアダプタのクラスです。

 @Override 
public void onBindViewHolder(ViewHolder holder, int position) { 

    StoreListCard storeListCard = storeListCards.get(position); 
    holder.tvStoreName.setText(storeListCard.getName()); 
    if(storeListCard .getWifi()) 
    { 
int id = getResources().getIdentifier("yourpackagename:drawable/" + imageName, null, null) 
    holder.ivWifiIcon.setImageResource(id); 
    } 
} 
+0

ありがとう、それは働いた! – JerryKo

+0

あなたを助けてうれしい –

0

好き応答

関連する問題