0

This application not showing data mysql and not error. please help me how to fix this

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

this is class Adapterdata

in this class getcount if i write return list_data.size(), it is showing null or error

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

how should i do? this code not showing data form mysql. no error but not showing

Azizah
  • 55
  • 1
  • 9
  • debug at first. – IntelliJ Amiya Nov 09 '20 at 07:08
  • @IntelliJAmiya not working – Azizah Nov 09 '20 at 07:09
  • 1.Must be sure that it is not local host url as it will not work in mobile device it will only work in emulator. 2. If your targeted os version is Android Pie or grater then than you have to add _android:usesCleartextTraffic="true"_ in your manifest file. 3. Move _Adapterdata adapter = new Adapterdata(MainActivity.this, list_data);mRecyclerView.setAdapter(adapter);_ out of *for* loop scope. – Piyush Nov 09 '20 at 07:24
  • are you shure you have parsed data correctly, i.e. `list_data` have at least 1 item? how many times `onBindViewHolder` gets called, maybe your `list_barang` layout have `visibility="gone"`? use `Log.d` - put more logs when parsing and inside adapter. besides `adapter` initation in `for` loop (should be after) your code seems to be fine – snachmsm Nov 09 '20 at 07:27
  • step 1,2 done and step 3 i try but still not work – Azizah Nov 09 '20 at 07:29
  • Can you share with list_barang.xml ? – Yeldar Nurpeissov Nov 09 '20 at 07:34

5 Answers5

0

Did you add internet permission to manifest?

<uses-permission android:name="android.permission.INTERNET" />
MoriX
  • 1
  • 2
  • {"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"}, – Azizah Nov 09 '20 at 07:12
  • add System.out.print for list_data.size on getItemCount – MoriX Nov 09 '20 at 07:16
  • it already fix at getitemcount, but still not showing – Azizah Nov 09 '20 at 07:24
  • add xml layout file – MoriX Nov 09 '20 at 07:31
0

simply I make activity using your code. and this code is working.

I don't use StringRequest and image library. So, If this code is not working,

you have to check

  1. get Data from server
  2. check image library

anyway my example code is here.

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


}

I didn't change adapter code and just make list_barang.xml file.

now this code is working

p.s] this is my test code so. i make this. I don't make 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>
S T
  • 1,068
  • 2
  • 8
  • 16
  • is Log.d("response ", response); working? please show log in Log.d("response ", response); – S T Nov 09 '20 at 07:24
  • working, showing thi {"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"} – Azizah Nov 09 '20 at 07:26
0

Try this:

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();
                   
Ruli
  • 2,592
  • 12
  • 30
  • 40
Yeldar Nurpeissov
  • 1,786
  • 14
  • 19
0

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>
Azizah
  • 55
  • 1
  • 9
0

this issue already done. because koneksi.php have html its cause data not showing

Azizah
  • 55
  • 1
  • 9