0

In my app I use grid view to display data. grid view is in a fragment.I already retrieve data from mysql database in Log cat but that json data not display in custom grid.

activity_product_list.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".DrawerActivity">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/header2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PRODUCTS : -"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@color/blue"/>

    <GridView
        android:id="@+id/productlist"
        android:layout_below="@+id/header2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:numColumns="2"
        android:horizontalSpacing="2dp"
        android:layout_marginTop="@dimen/activity_horizontal_margin">

    </GridView>

</RelativeLayout>

ProductListFragment.java

public class ProductListFragment extends Fragment {

List<Product> productList;
GridView gridView;
CustomProductList customProductList;
int categoryid;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_product_list, container,false);

    gridView = (GridView) rootView.findViewById(R.id.productlist);
    final GlobalVariable ID = (GlobalVariable)getActivity().getApplication();
    categoryid = ID.getCategoryid();

    productList = new ArrayList<>();
    loadProduct();

    customProductList = new CustomProductList(getActivity(),productList);
    gridView.setAdapter(customProductList);

    return rootView;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

private void loadProduct() {
    String PRODUCT_URL = "http://192.168.0.101/cart/product/get_all_product.php?vcategoryid="+categoryid;
    StringRequest stringRequest= new StringRequest(Request.Method.GET, PRODUCT_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject obj = new JSONObject(response);
                JSONArray array = obj.getJSONArray("products");
                for (int i = 0; i < array.length(); i++){
                    //getting the user from the response
                    JSONObject userJson = array.getJSONObject(i);
                    Product product = new Product();
                    product.setProductid(userJson.getInt("productid"));
                    product.setProductName(userJson.getString("productname"));
                    product.setProductDesc(userJson.getString("productdesc"));
                    product.setPrice(userJson.getString("productprice"));

                    productList.add(product);
                    Log.e("product",userJson+"");
                }
                customProductList.notifyDataSetChanged();
                Log.e("product",customProductList+"");
                //customCategoryList = new CustomCategoryList(getActivity(),categoryList);
                //recyclerView.setAdapter(customCategoryList);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    Volley.newRequestQueue(getActivity()).add(stringRequest);
}
}

custom_product_list.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@drawable/home_appliances"/>

<TextView
    android:id="@+id/productName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Redmi"
    android:textStyle="bold"
    android:textColor="@color/black"
    android:textSize="22sp"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="5dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RS."
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9999"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@color/blue"
        android:layout_marginLeft="10dp"/>
</LinearLayout>

CustomProductList.java

public class CustomProductList extends BaseAdapter {
private Context mCtx;
private List<Product> productList;

public CustomProductList(Context mCtx, List<Product> productList) {
    this.mCtx = mCtx;
    this.productList = productList;
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mCtx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {
        gridView = new View(mCtx);

        // get layout from mobile.xml
        gridView = inflater.inflate(R.layout.custom_product_list, null);

        TextView productname = (TextView) gridView.findViewById(R.id.productName);
        TextView price = (TextView) gridView.findViewById(R.id.price);

        // getting data for the row
        Product product = productList.get(position);
        // set data
        productname.setText(product.getProductName());
        price.setText(product.getPrice());
    } else {
        gridView = (View) convertView;
    }
    return gridView;
}
}

This is my code.All data comes from database in logcat as json..But All not display in grid.Please help me.Tell me What is the problem.

1 Answer 1

1

Solution:

You forgot to get count the updated list, hence chenge the below code:

@Override
public int getCount() {
    return 0;
}

to this:

@Override
public int getCount() {
    return this.productList.size();
}

That's it. Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Happy To Help. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.