Adapter's class getView() function not called for Android ListView

Muzzammil Hussain :

I am trying to create a ListView having a list of items, each having three components.

  1. Title (SezonNo)
  2. Description
  3. An Image (SezonThumb)

Following is the code for the layout file "layout_album", how will each item in the list be looked like:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    tools:context=".Activities.SeasonsActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/imgThumbnail"
            android:clickable="true"
           />
        <TextView
            style="@style/style_textviewsProfile"
            android:layout_margin="0dp"
            android:textStyle="bold"
            android:id="@+id/textViewSeasonNo"
            android:text="Season No:     " />
        <TextView
            android:id="@+id/textViewDescription"
            style="@style/style_textviewsProfile"
            android:text="Description:      " />
        <View
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:background="@color/Gray"></View>
    </LinearLayout>
</ScrollView>

The following is the code for the Activity:

  public void onCreate(){
     super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_seasons);
          listView_seasons=findViewById(R.id.list_seasons);

            txtTitle = findViewById(R.id.txtTitle);

            String[] seasonNos=new String[]{"1","2","3","4","5"};
            String[] seasonDescriptions=new String[]{"a","b","c","d","e"};
           int[] seasonThumbs= new int[]{R.drawable.sezon1, R.drawable.sezon2, R.drawable.flag, R.drawable.flag, R.drawable.flag};


      adapter=new ListAdapter(this,seasonNos,seasonDescriptions,seasonThumbs);
            listView_seasons.setAdapter(adapter);

    }
    //____________________________Adapter Class________________________//

      public class ListAdapter extends ArrayAdapter<String> {
            Context context;
            String sezonNo[];
            String desc[];
            int[] imgs;

            public ListAdapter(@NonNull Context context, String[] sezonNo, String[] desc, int[] imgs) {


                super(context,R.layout.layout_album,R.id.textViewSeasonNo,sezonNo);
                this.context=context;
                this.sezonNo = sezonNo;
                this.desc = desc;
                this.imgs = imgs;
                Log.d("List","Adapter");
            }

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


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

            @Override
            public View getView(int position, View view, ViewGroup parent) {
                Log.d("List","setting bindings...");
               view=getLayoutInflater().inflate(R.layout.layout_album,null);

                TextView txtSezonNo=view.findViewById(R.id.textViewSeasonNo);
                TextView txtDescription=view.findViewById(R.id.textViewDescription);

                ImageView imgThumbnail=view.findViewById(R.id.imgThumbnail);


                progressDialog.hide();

                txtSezonNo.setText(seasonNos[position]);
                txtDescription.setText(seasonDescriptions[position]);
                imgThumbnail.setImageResource(seasonThumbs[position]);

                return view;
            }
        }

The Constructor of Adapter class is getting called but getView() not called at all.

ismail alaoui :

your getView() is not called because the size you returned in getCount() is 0

please in your getCount() put this :

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

also getItemId should not be 0 for all, simple solution could be return position of item in the list. return position like

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=149878&siteId=1