我正在尝试根据我得到的JSON响应在我的cardview中设置文本和图像。我的json将包含以下信息:
{["name":"Store 1","wifi":true, ...]}
我正在使用片段,cardadapter和类,我现在可以解析名称并显示在我的cardview上但现在我想显示来自我的drawable文件的wifi图标,如果wifi的响应是真的并且不要#39;如果图标为假,则显示图标。
用于解析片段中数据的代码:
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"/>
我现在可以成功获取要显示的名称,但我不知道如何根据响应显示图标
答案 0 :(得分:1)
在onBindViewHolder里面做这个
@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);
}
}
答案 1 :(得分:0)
试试这个,
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);
}
}