gpt4 book ai didi

java - ListView CustomAdapter 不显示第一个位置结果

转载 作者:行者123 更新时间:2023-12-01 16:45:39 24 4
gpt4 key购买 nike

我是 Android Studio 和一般编码的新手,我正在尝试显示带有颜色图像和颜色名称的自定义 ListView。我使用了一个自定义适配器来使用两个包含可绘制图像颜色和名称字符串的数组来扩展 View 。

我遇到的问题 - 自定义适配器仅显示位置 1 及以后的图层。例如,第一层应该是 img_01 和“红色” - 它正在显示 img_02 和“橙色”。

我尝试调试代码,似乎虽然适配器正在设置位置 0 的值,但我找不到它不显示的原因(因为其他适配器显示正常)。

图像和名称只是占位符,因此不包含可绘制对象和名称的替代解决方案是不可行的,

提前谢谢

自定义适配器.java

public class CustomAdapter extends BaseAdapter {

Context context;
int[] images;
String[] colour;
LayoutInflater mInflater;

public CustomAdapter(Context c, int[] i, String[] col) {
this.context = c; //sets the application context that has been passed to the adapter
this.images = i; //sets the images array that has been passed to the adapter
this.colour = col; //sets the colour array that has been passed to the adapter
mInflater = (LayoutInflater.from(context)); //sets the layout inflater from context
}

@Override
public int getCount() { //total number of elements in the array
return images.length;
}

@Override
public Object getItem(int position) { //gets the item using the position
return null;
}

@Override
public long getItemId(int position) { //gets the item position
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) { //this is used to update the view

convertView = mInflater.inflate(R.layout.custom_layout, null);

ImageView image = convertView.findViewById(R.id.imageView);

TextView text = convertView.findViewById(R.id.textView);

text.setText(colour[position]);
image.setImageResource(images[position]);
return convertView;

}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

ListView mListView;

int[] images = {R.drawable.img01,
R.drawable.img02,
R.drawable.img03,
R.drawable.img04};

String[] colour = {"Red",
"Orange",
"Yellow",
"Green"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mListView = findViewById(R.id.listView);

CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), images, colour);
//passes information of images and application context to the item adapter
mListView.setAdapter(customAdapter);
//sets the adapter to the listview

}
}

自定义布局.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">


<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="7dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="9dp"
app:srcCompat="@drawable/img01" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="99dp"
android:layout_marginLeft="99dp"
android:layout_marginTop="36dp"
android:text="colourName" />

</RelativeLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

<ListView
android:id="@+id/listView"
android:layout_width="409dp"
android:layout_height="729dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

尝试将 ListView 的高度设置为 wrap_content 而不是对其进行硬编码

<ListView
...
android:layout_height="wrap_content"/>

关于java - ListView CustomAdapter 不显示第一个位置结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61783811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com