0

I have created a list of Products which are correctly populating in the CustomListAdaptor that I created but the View is not getting rendered when I am running the application.

In the below Adaptor, prodList is correctly getting populated as a Product entity

import android.app.Activity;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;


public class CustomProductListAdaptor extends ArrayAdapter {

    //to reference the Activity
    private final Activity context;

    private final List<Product> productList;


    public CustomProductListAdaptor(Activity context,List<Product> prodList){
        super(context,R.layout.productlist_item);
        this.context=context;
        this.productList = prodList;
    }

    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.productlist_item, null,true);
        Toast.makeText(getContext(),"In getView",Toast.LENGTH_LONG).show();

        //this code gets references to objects in the listview_row.xml file
        TextView nameTextField = (TextView) rowView.findViewById(R.id.product_name);
        TextView priceTextField = (TextView) rowView.findViewById(R.id.product_description);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.productImage);

        //this code sets the values of the objects to values from the list
        try {
            nameTextField.setText(productList.get(position).getName());
            priceTextField.setText(productList.get(position).getPrice()+"/-");
            Uri imageUri = Uri.parse(productList.get(position).getImgUrl());
            Picasso.with(context).load(imageUri).placeholder(R.drawable.selectphoto).into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rowView;
    }


}

1 Answers1

0

set the item view layout height to wrapcontent

set a layoutmanager to the recyclerview

moumenShobakey
  • 389
  • 4
  • 13
  • The item view layout height is already wrapcontent only. And instead of recycler view I am using the list view. Earlier I was getting the data, but through multiple arrays. Now I have modified the logic to return a Product list. The same is coming to the adaptor but its not getting rendered. I guess some part is not working from the getView method. – anshul goel Mar 17 '20 at 11:12
  • Did you check the logat maybe it catches url parse error – moumenShobakey Mar 17 '20 at 11:14
  • In logcat I am getting an Exception as _java.lang.NullPointerException: Attempt to get length of null array_ but I debugged the flow and the data is coming to the adaptor. Is there a way in which I can debug the getView method? I wrote 1 toast message but that is also not coming. – anshul goel Mar 17 '20 at 11:20
  • Can you post the logat? – moumenShobakey Mar 17 '20 at 11:54
  • Invalid drawable added to LayerDrawable! Drawable already belongs to another owner but does not expose a constant state. java.lang.RuntimeException at android.graphics.drawable.LayerDrawable$ChildDrawable.(LayerDrawable.java:1857) at android.graphics.drawable.LayerDrawable$LayerState.(LayerDrawable.java:1977) – anshul goel Mar 17 '20 at 12:12
  • where do you use this drawable ? :/ the problem isn't in the getView then! – moumenShobakey Mar 17 '20 at 18:28
  • I have modified the constructor's super call with an additional parameter of listview and its working fine now. – anshul goel Mar 17 '20 at 18:30