user129923
user129923

Reputation: 13

Custom listview is not showing and getView is not called

I have problem that my list view is not showing and the getView method never invoked

here is my list view inside the onCreate in the MainActivity

l = (ListView) findViewById(R.id.listvv);
        CustomAdapter adapter = new CustomAdapter(MainActivity.this, maainimg);

        l.setAdapter(adapter);

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="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:orientation="vertical"
android:background="@color/white"
    tools:context=".MainActivity">

   <ListView
       android:id="@+id/listvv"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:divider="#b4be"
       android:dividerHeight="2dp"></ListView>

    <LinearLayout
        android:id="@+id/ad_holder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center|bottom"

        android:layout_alignParentBottom="true">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            ads:adSize="BANNER"
            ads:adUnitId="@string/BottomBanner">
        </com.google.android.gms.ads.AdView>
    </LinearLayout>






</LinearLayout>

and here is my CustomAdapter Class it is inside the MainActivity

   public class CustomAdapter extends ArrayAdapter<String> {
        int[] img;
        Activity activity;
        ImageButton imageButton;
        public CustomAdapter(Activity act, int [] images) {
            super(act, R.layout.custom_row);
            this.activity=act;
            this.img=images;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Log.d("getView","Called!");

            View row = convertView;
            if(row==null){
                LayoutInflater inflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.custom_row, parent, false);

                imageButton   = (ImageButton) row.findViewById(R.id.preview);}

            imageButton.setImageResource(img[position]);
            Log.d("added",position+"");
            if (getSelectedcolor() == 0) {
                imageButton.setColorFilter(Color.RED);
                setSelectedcolor(Color.RED);

            } else {
                imageButton.setColorFilter(Color.parseColor(colorToHexString(getSelectedcolor())));

            }

            return row;
        }
    }

and the custom_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background">
    <ImageButton
        android:id="@+id/preview"
        android:layout_width="80dp"
        android:layout_height="80dp"
          android:layout_marginLeft="20dp"
       android:layout_marginTop="10dp"
        android:src="@drawable/ic_1_red"
        android:scaleType="fitXY"
        android:background="@drawable/roundedbutton"

        />

</LinearLayout>

Update here is my array maainimg its not empty

 maainimg = new int[]{R.drawable.ic_1_red,R.drawable.ic_2_red,R.drawable.ic_3_red,R.drawable.ic_4_red,R.drawable.ic_5_red,R.drawable.ic_6_red,
                R.drawable.ic_7,R.drawable.ic_8,R.drawable.ic_8,R.drawable.ic_9,R.drawable.ic_10,R.drawable.ic_11,R.drawable.ic_12
                ,R.drawable.ic_13,R.drawable.ic_14,R.drawable.ic_15,R.drawable.ic_16,R.drawable.ic_17,R.drawable.ic_18,R.drawable.ic_19,R.drawable.ic_20,
                R.drawable.ic_21,R.drawable.ic_22,R.drawable.ic_23,R.drawable.ic_24,R.drawable.ic_25};

Upvotes: 0

Views: 72

Answers (3)

diyoda_
diyoda_

Reputation: 5420

I this there is a issue in your Adapter class, Try this, But keep in mind that there are cleaner ways of writing this same code,

public class CustomAdapter extends ArrayAdapter<Integer> {
        int[] img;
        Context context;
        ImageButton imageButton;
        public CustomAdapter(Context context, Integer [] images) {
            super(context, R.layout.custom_row, images);
            this.context=context;
            this.img=images;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Log.d("getView","Called!");

            View row = convertView;
            if(row==null){
                LayoutInflater inflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.custom_row, parent, false);

            }

            imageButton   = (ImageButton) row.findViewById(R.id.preview);
            imageButton.setImageResource(img[position]);
            Log.d("added",position+"");
            if (getSelectedcolor() == 0) {
                imageButton.setColorFilter(Color.RED);
                setSelectedcolor(Color.RED);

            } else {
                imageButton.setColorFilter(Color.parseColor(colorToHexString(getSelectedcolor())));

            }

            return row;
        }
    }

Upvotes: 1

Martin Marconcini
Martin Marconcini

Reputation: 27236

The first problem I see is your Layout.

   <ListView
       android:id="@+id/listvv"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:divider="#b4be"
       android:dividerHeight="2dp"></ListView>

    <LinearLayout
        android:id="@+id/ad_holder"
        android:layout_width="fill_parent"

Your ListView has no room left because the bottom linear layout is using FILL_PARENT (which is deprecated btw, use MATCH_PARENT instead).

Try making your ListView height (and width) to be both MATCH_PARENT. And then deal with the "Ad".

You could have the LinearLayout replaced by a RelativeLayout and have the Ad pin at the bottom of its parent and below the listview.

Second: Why are you using an ArrayAdapter<String> if you're internally using an array of int.

Third: since you're using the wrong type of adapter, you're not correctly implementing it, you need to override more methods (getCount() for example), to tell the underlying adapter: hey, this is the number of items we have.

Since you're not using the provided array of strings, but your custom array of ints that you pass during construction, try overriding getCount() and return the size of your int array instead.

Upvotes: 1

Rami
Rami

Reputation: 7929

While you call super(act, R.layout.custom_row); (without passing the objects), you need to override getCount() method, otherwise your adapter can't figure out how many items you have, and this is why there is no call of getView() method.

@Override
public int getCount(){
   return img.length;
}

Upvotes: 1

Related Questions