数据 Mysql 未显示在 recyclerview 中

Data Mysql not showing in recyclerview

此应用程序未显示数据 mysql 并且没有错误。请帮我 如何解决这个问题

这是class MainActivity

public class MainActivity extends AppCompatActivity {
    private static final String data_url = "http://192.168.1.9/uasazizah/android.php";
    RecyclerView mRecyclerView;
    RecyclerView.Adapter mAdapter;
    RecyclerView.LayoutManager mManager;
    ProgressDialog pd;
    ArrayList<Modeldata> mItems;
    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_main);

        String url = "http://192.168.1.9/uasazizah/android.php";

        mRecyclerView = findViewById(R.id.bararecy);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        Adapterdata adapterdata = new Adapterdata(MainActivity.this, list_data);
        mRecyclerView.setAdapter(adapterdata);
        requestQueue = Volley.newRequestQueue(MainActivity.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("azizah");
                    for (int a = 0; a < jsonArray.length(); a++) {
                        JSONObject json = jsonArray.getJSONObject(a);
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("code", json.getString("code"));
                        map.put("groupname", json.getString("groupname"));
                        map.put("namag", json.getString("namag"));
                        list_data.add(map);
                        Adapterdata adapter = new Adapterdata(MainActivity.this, list_data);
                        mRecyclerView.setAdapter(adapter);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        requestQueue.add(stringRequest);
    }
}

这是 class 适配器数据

在这个 class getcount 中,如果我写 return list_data.size(),它显示为空或错误

public class Adapterdata extends RecyclerView.Adapter<Adapterdata.ViewHolder> {

    Context context;
    ArrayList<HashMap<String, String>> list_data;

    public Adapterdata(MainActivity mainActivity, ArrayList<HashMap<String, String>> list_data) {
        this.context = mainActivity;
        this.list_data = list_data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_barang, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DrawableCrossFadeFactory factory =
                new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();
        Glide.with(context)
                .load("http://192.168.1.9/uasazizah/images/" + list_data.get(position).get("namag"))
                .transition(withCrossFade())
                .apply(new RequestOptions().override(100, 100)
                        .placeholder(R.drawable.ic_launcher_background)
                        .error(R.drawable.ic_launcher_background).centerCrop()
                )
                .into(holder.img);
        holder.txtkate.setText(list_data.get(position).get("groupname"));
        holder.tkscode.setText(list_data.get(position).get("code"));
    }

    @Override
    public int getItemCount() {
        return list_data == null ? 0 : list_data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtkate,tkscode;
        ImageView img;

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

            txtkate = itemView.findViewById(R.id.namatvv);
            tkscode = itemView.findViewById(R.id.code);
            img = itemView.findViewById(R.id.imgv);
        }
    }
}

我该怎么办?此代码未显示数据形式 mysql。没有错误但没有显示

您是否为清单添加了互联网权限?

<uses-permission android:name="android.permission.INTERNET" />

只是我使用您的代码 activity。这段代码正在运行。

我不使用 StringRequest 和图像库。所以,如果这段代码不起作用,

你必须检查

  1. 从服务器获取数据
  2. 查看图片库

反正我的示例代码在这里。

public class MainActivity extends AppCompatActivity {

    RecyclerView mRecyclerView;
    Adapterdata adapterdata;
    ArrayList<HashMap<String, String>> list_data;

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

        mRecyclerView = findViewById(R.id.bararecy);
        list_data = new ArrayList<HashMap<String, String>>();

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapterdata = new Adapterdata(MainActivity.this, list_data);
        mRecyclerView.setAdapter(adapterdata);

        String test = "{\"azizah\":[{\"0\":\"116\",\"code\":\"116\",\"1\":\"desk\",\"description\":\"desk\",\"2\":\"BBM\",\"groupname\":\"BBM\",\"3\":\"gajahh.jpg\",\"namag\":\"gajahh.jpg\"},{\"0\":\"123\",\"code\":\"123\",\"1\":\"test\",\"description\":\"test\",\"2\":\"Pupuk\",\"groupname\":\"Pupuk\",\"3\":\"jerapah.jpg\",\"namag\":\"jerapah.jpg\"},{\"0\":\"1234\",\"code\":\"1234\",\"1\":\"Hei\",\"description\":\"Hei\",\"2\":\"BBM\",\"groupname\":\"BBM\",\"3\":\"case.PNG\",\"namag\":\"case.PNG\"},{\"0\":\"222\",\"code\":\"222\",\"1\":\"aas\",\"description\":\"aas\",\"2\":\"Pupuk\",\"groupname\":\"Pupuk\",\"3\":\"harimau.jpg\",\"namag\":\"harimau.jpg\"}]} ";
        try {
            JSONObject jsonObject = new JSONObject(test);
            JSONArray jsonArray = jsonObject.getJSONArray("azizah");
            for (int a = 0; a < jsonArray.length(); a++) {
                JSONObject json = jsonArray.getJSONObject(a);
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("code", json.getString("code"));
                map.put("groupname", json.getString("groupname"));
                map.put("namag", json.getString("namag"));
                list_data.add(map);

            }

            adapterdata.notifyDataSetChanged();

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


}

我没有更改适配器代码,只是制作 list_barang.xml 文件。

现在这段代码可以工作了

p.s]这是我的测试代码所以。我做这个。我不做ImageView。

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

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

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

</LinearLayout>

试试这个:

for (int a = 0; a < jsonArray.length(); a++) {
    JSONObject json = jsonArray.getJSONObject(a);
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("code", json.getString("code"));
    map.put("groupname", json.getString("groupname"));
    map.put("namag", json.getString("namag"));
    list_data.add(map);
 }
 Adapterdata adapter = new Adapterdata(MainActivity.this, list_data);
 mRecyclerView.setAdapter(adapter);
 adapter.notifyDataSetChanged();
                   

list_barang.xml

 <?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardUseCompatPadding="true"
    app:cardCornerRadius="2dp"
    app:cardElevation="2dp"
    app:contentPadding="3dp"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="106dp"
        android:background="#FFFFFF"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imgv"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher_background" />

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

            <TextView
                android:id="@+id/namatvv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:text="Kategori"
                android:textColor="@android:color/background_dark"
                android:textSize="12sp"
                android:textStyle="bold"
                app:fontFamily="@font/acme" />

            <TextView
                android:id="@+id/code"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:text="Code"
                android:textColor="@android:color/background_dark"
                android:textSize="8sp"
                android:textStyle="bold"
                app:fontFamily="@font/acme" />

        </LinearLayout>


    </LinearLayout>

</androidx.cardview.widget.CardView>

activity_main

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="#FFFF"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="36dp"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:fontFamily="@font/acme"
        android:text="Data Barang"
        android:textColor="@android:color/background_dark"
        android:textSize="30sp"
        android:textStyle="bold"
        app:fontFamily="@font/acme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <androidx.recyclerview.widget.RecyclerView

        android:id="@+id/bararecy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="48dp"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="56dp"
        android:layout_marginEnd="48dp"
        android:layout_marginRight="48dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3">

    </androidx.recyclerview.widget.RecyclerView>


</androidx.constraintlayout.widget.ConstraintLayout>

这个问题已经解决了。因为 koneksi.php 有 html 其原因数据未显示