Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-图像和文本不会加载(RecylerView-CardView-API)_Java_Android_Android Studio_Android Recyclerview_Android Cardview - Fatal编程技术网

Java Android-图像和文本不会加载(RecylerView-CardView-API)

Java Android-图像和文本不会加载(RecylerView-CardView-API),java,android,android-studio,android-recyclerview,android-cardview,Java,Android,Android Studio,Android Recyclerview,Android Cardview,我是Android Studio的初学者,这是我的第一个应用程序, 问题:当我启动应用程序时,使用可绘制和本地文本中的图像以及RecyclerView和CardView可以很好地工作 但是当我使用API JSON时,应用程序不会显示任何内容 这是我的密码 主要活动 public class MainActivity extends AppCompatActivity { List<category> lstcategory; @Override protected void on

我是Android Studio的初学者,这是我的第一个应用程序, 问题:当我启动应用程序时,使用可绘制和本地文本中的图像以及RecyclerView和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);

}
RecycleServiceAdapter

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

    }
}
]


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

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

lstcategory.add(cat);// Add it to the end of the try section
当前,您正在向RecyclerView传递一个空列表,这就是为什么它不显示任何项目,因为列表中没有元素


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

我尝试了您的解决方案,但仍然没有显示任何内容:(无论如何,感谢您抽出时间
    <?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>
[
{
    "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"
}
lstcategory.add(cat);// Add it to the end of the try section