android recyclerview没有显示任何内容

时间:2017-05-11 06:32:21

标签: android json android-recyclerview adapter

我正在使用RecyclerView和自定义适配器,但它没有显示数据。

adapter.class的代码

public class AdapterList extends RecyclerView.Adapter<AdapterList.ViewHolder> {
    Context context;
    ArrayList<HashMap<String, String>> list_data;

    public AdapterList(Appetizer1 appetizer1, ArrayList<HashMap<String,String>> list_data){
        this.context = appetizer1;
        this.list_data = list_data;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_iitem,null);

        return new ViewHolder(view) ;
    }

    @Override
    public void onBindViewHolder(AdapterList.ViewHolder holder, int position) {
        Glide.with(context).load("http://kelompokdua.hol.es/pic/" + list_data.get(position)
        .get("gambar")).crossFade().placeholder(R.mipmap.ic_launcher)
                .into(holder.imgfood);
        holder.txtfood.setText(list_data.get(position).get("nama"));
        holder.txtid.setText(list_data.get(position).get("id"));
        holder.txtharga.setText(list_data.get(position).get("harga"));

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView txtfood,txtid,txtharga;
        ImageView imgfood;

        public ViewHolder (View itemView){
            super(itemView);

            txtfood = (TextView) itemView.findViewById(R.id.tv_nama);
            txtid = (TextView) itemView.findViewById(R.id.tv_id);
            txtharga = (TextView) itemView.findViewById(R.id.tv_harga);
            imgfood = (ImageView) itemView.findViewById(R.id.thumbnail);

        }
    }
}

mainactivity.class的代码

public class Appetizer1 extends AppCompatActivity {

    private RecyclerView lvfood;
    private RequestQueue requestQueue;
    private StringRequest stringRequest;

    ArrayList<HashMap<String, String>> list_data;

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

        String url = "http://kelompokdua.hol.es/private_html/show.php";
        lvfood = (RecyclerView) findViewById(R.id.rv1);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        lvfood.setLayoutManager(llm);

        requestQueue = Volley.newRequestQueue(Appetizer1.this);
        list_data = new ArrayList<HashMap<String, String>>();

        stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("response", response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("");
                    for (int a = 0; a < jsonArray.length(); a++) {
                        JSONObject json = jsonArray.getJSONObject(a);
                        HashMap<String, String> map = new HashMap<String, String>();

                        map.put("id", String.valueOf(json.getInt("id")));
                        map.put("nama", json.getString("nama"));
                        map.put("harga", String.valueOf(json.getInt("harga")));
                        map.put("gambar", json.getString("gambar"));
                        list_data.add(map);
                        AdapterList adapter = new AdapterList(Appetizer1.this, list_data);
                        lvfood.setAdapter(adapter);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(Appetizer1.this, error.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
        requestQueue.add(stringRequest);

    }}

这是我的json结果:

[[{"id":"1","nama":"Martabak","harga":"25000","gambar":"http:\/\/kelompokdua.hol.es\/pic\/martabak.jpg"},{"id":"2","nama":"Ayam Goreng","harga":"20000","gambar":"http:\/\/kelompokdua.hol.es\/pic\/ayam.jpg"},{"id":"3","nama":"Nasi Goreng","harga":"15000","gambar":"http:\/\/kelompokdua.hol.es\/pic\/nasigor.jpg"},{"id":"4","nama":"Cola","harga":"5000","gambar":"http:\/\/kelompokdua.hol.es\/pic\/cocacola.jpg"},{"id":"5","nama":"Es teh manis","harga":"3000","gambar":"http:\/\/kelompokdua.hol.es\/pic\/esteh.jpg"},{"id":"6","nama":"Jus","harga":"18000","gambar":"http:\/\/kelompokdua.hol.es\/pic\/juice.jpg"},{"id":"7","nama":"Pasta","harga":"20000","gambar":"http:\/\/kelompokdua.hol.es\/pic\/pasta.jpg"}]]

这是我的adapterlist.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/cardView">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="match_parent"
                android:layout_height="167dp"
                app:srcCompat="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/tv_id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/tv_nama"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/tv_harga"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

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

</RelativeLayout>

和这个mainactivity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.kelompok3.restaurantapp.restoranfixx.Isimenu.Appetizer1">

        <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kelompok3.restaurantapp.restoranfixx.Isimenu.Appetizer1">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv1"
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:visibility="visible"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />

</RelativeLayout>

我没有在logcat上看到任何错误,我很困惑。它只显示一个空白的布局。我改变了能见度但没有发生任何事情。我现在尝试任何建议后问题是

E/RecyclerView: No adapter attached; skipping layout

6 个答案:

答案 0 :(得分:0)

您的代码看起来很完美。只需检查您的Recyclerview在XML文件中是否可见。可能是你错误地使你的能见度消失了。只需检查并告诉我,或者您可以通过以下代码进行检查

AdapterList adapter = new AdapterList(Appetizer1.this, list_data);
lvfood.setAdapter(adapter);
lvfood.setVisibility(View.VISIBLE);

答案 1 :(得分:0)

显示recyclelerview的可见性,如下所示: -

lvfood.setVisibility(View.VISIBLE);
lvfood.setAdapter(adapter);

答案 2 :(得分:0)

将以下行移出for循环

AdapterList adapter = new AdapterList(Appetizer1.this, list_data);
lvfood.setAdapter(adapter);

另外请确保列表在xml中可见。

lvfood.setVisibility(View.VISIBLE);

答案 3 :(得分:0)

我会尝试移动这两行:

AdapterList adapter = new AdapterList(Appetizer1.this, list_data);
lvfood.setAdapter(adapter);

您应该将它们从for循环移出,否则您为该数组中的每个JSONObject创建并设置一个新的Adapter。 我还建议创建一个包含JSONObject属性的类,这样就可以使用List而不是HashMap。

另外,正如其他人建议的那样,尝试检查RecyclerView可见性:

lvfood.setVisibility(View.VISIBLE);

答案 4 :(得分:0)

问题出在Glide的链接中。您正在存储JSON的完整链接。但是当您尝试加载该图像时,您还会添加标题链接,这就是图像无法加载的原因。 只需像这样更改Gilde链接即可。 Glide.with(context).load(list_data.get(position).get("gambar")).crossFade().placeholder(R.mipmap.ic_launcher).into(holder.imgfood);

答案 5 :(得分:0)

将此代码替换为响应()方法

 try {              

                JSONArray response1=new JSONArray(response);
                JSONArray jsonArray = response1.getJSONArray(0);
                for (int a = 0; a < jsonArray.length(); a++) {
                    JSONObject json = jsonArray.getJSONObject(a);
                    try {
                        HashMap<String, String> map = new HashMap<String, String>();


                        map.put("id", String.valueOf(json.getInt("id")));
                        map.put("nama", json.getString("nama"));
                        map.put("harga", String.valueOf(json.getInt("harga")));
                        map.put("gambar", json.getString("gambar"));
                        list_data.add(map);

                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }


                }
                AdapterList adapter = new AdapterList(getApplicationContext(), list_data);
                lvfood.setAdapter(adapter);
            }catch (JSONException e) {
            e.printStackTrace();
        }

并在Arraylist中宣布

 ArrayList<HashMap<String, String>> list_data=new ArrayList<>();