根据 JSON 响应在 cardview 中设置图像
Set Image in cardview from JSON response
我正在尝试根据收到的 JSON 响应在卡片视图中设置文本和图像。我的 json 将包含以下信息:
{["name":"Store 1","wifi":true, ...]}
我正在为项目使用片段、cardadapter 和 class 我现在可以解析名称并显示在我的 cardview 上,但现在我想显示我的可绘制文件中的 wifi 图标,如果响应for wifi 为 true,如果为 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;
}
}
卡适配器:
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"/>
我现在可以成功显示名称,但我不确定如何根据响应显示图标
在 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);
}
}
试试这个,
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);
}
}
在卡适配器中class。
@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);
}
}
我正在尝试根据收到的 JSON 响应在卡片视图中设置文本和图像。我的 json 将包含以下信息:
{["name":"Store 1","wifi":true, ...]}
我正在为项目使用片段、cardadapter 和 class 我现在可以解析名称并显示在我的 cardview 上,但现在我想显示我的可绘制文件中的 wifi 图标,如果响应for wifi 为 true,如果为 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;
}
}
卡适配器:
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"/>
我现在可以成功显示名称,但我不确定如何根据响应显示图标
在 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);
}
}
试试这个,
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);
}
}
在卡适配器中class。
@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);
}
}