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



我是Android Studio的初学者,这是我的第一个应用程序, 问题:当我使用可绘制和本地文本中的图像与回收器视图和CardView启动应用程序时工作正常 但是当我使用 API JSON 时,应用程序无需显示任何内容即可工作 这是我的代码

主要活动

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

回收器视图适配器

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

这是我的布局活动主

<?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>

卡片视图布局

<?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>

接口 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 块中,您需要将 cat 对象添加到 lstcategory List。所以你基本上应该这样做:

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

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

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

最新更新