gpt4 book ai didi

java - Android - 图像和文本无法加载(RecyclerView - CardView - API)

转载 作者:行者123 更新时间:2023-12-02 11:07:49 24 4
gpt4 key购买 nike

我是 Android Studio 的初学者,这是我的第一个应用程序,问题:当我使用来自 Drawable 的图像和带有 RecyclerView 和 CardView 的本地文本启动应用程序时,效果很好但是当我使用 API JSON 时,应用程序可以正常工作而不显示任何内容这是我的代码

主要 Activity

public class MainActivity extends AppCompatActivity {

List<category> lstcategory;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


lstcategory = new ArrayList<>();
AndroidNetworking.initialize(getApplicationContext());

AndroidNetworking.get("https://api.myjson.com/bins/7r486")
.setPriority(Priority.LOW)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
category cat = new category();
try{
JSONArray pj = response.getJSONArray("category");

for (int i=0;i< pj.length();i++)
{
JSONObject jb = pj.getJSONObject(i);
// cat.setImgURL(pj.getJSONObject(i).getString("imgUrl"));
// cat.setLibelle(pj.getJSONObject(i).getString("libelle"));
cat.setLibelle(jb.getString("libelle"));
cat.setImgURL(jb.getString("imgUrl"));

}

}
catch(JSONException e)
{
e.printStackTrace();
}
lstcategory.add(cat);// Add it to the end of the try section
}

@Override
public void onError(ANError anError) {

}
});



RecyclerView myrv = (RecyclerView) findViewById(R.id.recyclerview_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,lstcategory);
myrv.setLayoutManager(new GridLayoutManager(this,3));
myrv.setAdapter(myAdapter);

}

}

类别模态

public class category {

private String libelle;
private String imgURL;

public category() {
}

public String getLibelle() {
return libelle;
}

public String getImgURL() {
return imgURL;
}

public category(String libelle, String imgURL) {
this.libelle = libelle;
this.imgURL = imgURL;
}

public void setLibelle(String libelle) {
this.libelle = libelle;
}

public void setImgURL(String imgURL) {
this.imgURL = imgURL;
}

RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

private Context mContext;
private List<category> mData;

public RecyclerViewAdapter(Context mContext, List<category> mData) {
this.mContext = mContext;
this.mData = mData;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.item_posts,parent,false);
return new MyViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.tv_libelle.setText(mData.get(position).getLibelle());
holder.tv_imgURL.setText(mData.get(position).getImgURL());

}

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

}

public static class MyViewHolder extends RecyclerView.ViewHolder
{
TextView tv_libelle;
TextView tv_imgURL;

public MyViewHolder(View itemView) {
super(itemView);
tv_libelle = (TextView) itemView.findViewById(R.id.libelle_layout);
tv_imgURL = (TextView) itemView.findViewById(R.id.imgURL_layout);

}
}

这是我的布局主要 Activity

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview_id"
android:layout_width="match_parent"
android:layout_height="match_parent">


</android.support.v7.widget.RecyclerView>

</RelativeLayout>

CardView布局

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp"
>


<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imgURL_layout"
android:layout_width="100dp"
android:layout_height="90dp"
android:scaleType="centerCrop"
android:background="#2d2d2d"

/>
<TextView
android:id="@+id/libelle_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test"
android:gravity="center"
/>

</LinearLayout>


</android.support.v7.widget.CardView>

API JSON

[
{
"id": 1,
"libelle": "Développement pour l'entreprise",
"description": "",
"imgUrl": "https://s3-eu-west-1.amazonaws.com/course.oc-static.com/categories/12/14599616875602_dev-entreprise.svg"
},
{
"id": 2,
"libelle": "Initiation numérique",
"description": "",
"imgUrl": "https://s3-eu-west-1.amazonaws.com/course.oc-static.com/categories/13/14599617123358_initiation-numerique.svg"
},
{
"id": 3,
"libelle": "Marketing digital",
"description": "",
"imgUrl": "https://s3-eu-west-1.amazonaws.com/course.oc-static.com/categories/14/14599617408214_marketing-digital.svg"
},
{
"id": 4,
"libelle": "Do it yourself",
"description": "",
"imgUrl": "https://s3-eu-west-1.amazonaws.com/course.oc-static.com/categories/15/14599617668172_diy.svg"
},
{
"id": 5,
"libelle": "Gestion de projet",
"description": "",
"imgUrl": "https://s3-eu-west-1.amazonaws.com/course.oc-static.com/categories/16/14599618101434_gestion-projet.svg"
},
{
"id": 6,
"libelle": "Entrepreneuriat",
"description": "",
"imgUrl": "https://s3-eu-west-1.amazonaws.com/course.oc-static.com/categories/17/14599618766968_entreprise.svg"
},
{
"id": 7,
"libelle": "Développement web",
"description": "",
"imgUrl": "https://s3-eu-west-1.amazonaws.com/course.oc-static.com/categories/18/14599618519803_developpement.svg"
},
{
"id": 8,
"libelle": "Développement mobile",
"description": "",
"imgUrl": "https://s3-eu-west-1.amazonaws.com/course.oc-static.com/categories/19/14599619144036_dev-mobile.svg"
},
{
"id": 9,
"libelle": "Développement personnel",
"description": "",
"imgUrl": "https://s3-eu-west-1.amazonaws.com/course.oc-static.com/categories/20/14599619404905_dev-perso.svg"
},
{
"id": 10,
"libelle": "Design",
"description": "",
"imgUrl": "https://s3-eu-west-1.amazonaws.com/course.oc-static.com/categories/6/14599619912146_design.svg"
},
{
"id": 11,
"libelle": "Systèmes et réseaux",
"description": "",
"imgUrl": "https://s3-eu-west-1.amazonaws.com/course.oc-static.com/categories/21/14599619648729_systeme-reseau.svg"
},
{
"id": 22,
"libelle": "Autre",
"description": "",
"imgUrl": "http://static.oc-static.com/prod/images/categories/default_category_icon.svg"
}

]

我的目标是应用程序从 API 获取信息并显示它们!

最佳答案

这是因为您没有将类别对象添加到适配器中。在设置 cat 对象属性的 try-catch block 中,您需要将 cat 对象添加到 lstcategory 列表中。所以你基本上应该这样做:

lstcategory.add(cat);// Add it to the end of the try section

目前,您正在向 RecyclerView 传递一个空列表,这就是为什么它不显示任何项目,因为列表中没有元素。

除此之外,我建议您将类名称的第一个字母大写,因为这是标准约定,并且更易于阅读。

关于java - Android - 图像和文本无法加载(RecyclerView - CardView - API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50823210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com