未为Android ListView调用适配器的类getView()函数

时间:2019-04-18 17:57:26

标签: java android xml layout android-listview

我正在尝试创建一个具有项目列表的ListView,每个项目都有三个组件。

  1. 标题(SezonNo)
  2. 说明
  3. 图像(SezonThumb)

以下是布局文件“ layout_album”的代码,列表中的每个项目看起来如何:

<?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>

以下是该活动的代码:

  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;
            }
        }

正在调用Adapter类的Constructor,但根本没有调用getView()

1 个答案:

答案 0 :(得分:2)

因为您在getView()中返回的大小为0,所以未调用您的getCount()

请在您的getCount()中输入:

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

getItemId也不应该全部都是0,简单的解决方法是将项目返回列表中的位置。像

一样返回position
@Override
public long getItemId(int position) {
    return position;
}