首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android -无法加载图像和文本(RecylerView - CardView - API)

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

Stack Overflow用户
提问于 2018-06-13 02:03:36
回答 1查看 492关注 0票数 0

我是Android Studio的初学者,这是我的第一个应用程序,问题是:当我使用RecyclerView和CardView从可绘制的图像和本地文本启动应用程序时,运行正常,但当我使用API JSON时,应用程序运行时不会在这里显示我的代码

主活动

代码语言:javascript
复制
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);

}

}

类别模态

代码语言:javascript
复制
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

代码语言:javascript
复制
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);

    }
}

Here my Layout Activity Main

代码语言:javascript
复制
    <?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 Layout

代码语言:javascript
复制
 <?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

代码语言:javascript
复制
[
{
    "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获取信息并显示它们!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-13 02:11:51

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

代码语言:javascript
复制
lstcategory.add(cat);// Add it to the end of the try section

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50823210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档