Question

It probably will be simple answer but please take a look at this error. I want to make adapter to return a View. I've been looking everywhere in network but still can not make it working. Please help me.

Here is my code:

MAIN ACTIVITY shortly:

public void setAdapter()
    {
        adapter = new ListAdapter(this, R.id.list_item, list);

    } 

AND ADAPTER:

public class ListAdapter extends ArrayAdapter<GifModel> {


    private ArrayList<GifModel> objects;
    private Context context;


    public ListAdapter(Context context, int textViewResourceId, ArrayList<GifModel> objects) {
        super(context, textViewResourceId, objects);
        this.objects = objects;
        this.context = context;
    }


    public View getView(int position, View convertView, ViewGroup parent){

        View v = convertView;


        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             // HERE IS A CRITICAL ERROR. IT CAN NOT INFLATE THIS LAYOUT AND DO NOT KNOW WHY
            v = inflater.inflate(R.layout.newListItem, null);
        }


        GifModel i = objects.get(position);

        if (i != null) {


            TextView tt = (TextView) v.findViewById(R.id.nameOfTheItem);
            TextView ttd = (TextView) v.findViewById(R.id.dateOfAdded);


            if (tt != null){
                tt.setText(i.getNameOfIteme());
            }
            if (ttd != null){
                ttd.setText(i.getAddedDate());
            }

        }

        // the view must be returned to our activity
        return v;

    }

END LIST ITEM VIEW:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@android:color/darker_gray"
    android:id="@+id/newListItem" >

    <ImageView
        android:id="@+id/listImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="19dp"
        android:layout_marginTop="16dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/dateOfAdded"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/listImageView"
        android:layout_marginLeft="26dp"
        android:layout_toRightOf="@+id/listImageView"
        android:text="HAHA" />

    <TextView
        android:id="@+id/nameOfTheItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/dateOfAdded"
        android:layout_alignTop="@+id/listImageView"
        android:text="UMCUMC" />

    <ImageView
        android:id="@+id/ratingView"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/nameOfTheItem"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
Was it helpful?

Solution

you can not use camel cased name for layout file. The only allowed chars are a-z,0-9,_

OTHER TIPS

It's also a good idea to declare and initialize your LayoutInflater in the constructor of the adapter, getView gets called at unpredictable times and declaring the object for it every time can lead to a lot of memory overhead.

I usually setup my adapters like this:

public MyAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;

    public MyAdapter(Context context) {
        layoutInflater = (LayoutInflater) context.getSystemsService(Context.LAYOUT_INFLATER_SERVICE);
    }

    // getId,etc...
}

Try replacing

(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

with

((Activity) context).getLayoutInflater();

Have you tried to use this other inflater instead?

inflater.inflate(R.layout.newListItem, parent, false);

I had a similar problem, but this solved it.

add interface ViewHolder inside of adapter. You can look up how to do it. and about your error, this should work:

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.YourAdapterLayoutt, parent, false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top