ListView CustomAdapter 未显示第一个位置结果
Posted
技术标签:
【中文标题】ListView CustomAdapter 未显示第一个位置结果【英文标题】:ListView CustomAdapter not showing first position results 【发布时间】:2020-08-30 04:30:44 【问题描述】:我是 android Studio 和一般编码的新手,我正在尝试显示带有颜色图像和颜色名称的自定义 ListView。我使用了一个自定义适配器来使用两个包含可绘制图像颜色和名称字符串的数组来扩展视图。
我遇到的问题 - 自定义适配器仅显示位置 1 及以后的层。例如,第一层应该是 img_01 和“红色” - 它显示的是 img_02 和“橙色”。
我已经尝试调试代码,似乎虽然适配器正在设置位置 0 的值,但我找不到它不显示的原因(因为其他显示正常)。
图像和名称只是占位符,因此不包括可绘制对象和名称的替代解决方案是不可行的,
提前谢谢你
CustomAdapter.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
custom_layout.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_ android:layout_>
<ImageView
android:id="@+id/imageView"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
【参考方案1】:尝试将ListView
的高度设置为wrap_content
而不是硬编码
<ListView
...
android:layout_/>
【讨论】:
【参考方案2】:你可以用 RecyclerView 做到这一点。
-
创建自定义类
public class ExampleItem
private int mImageResource; //your color image
private String mText1; //your text
public ExampleItem(int imageResource, String text1)
mImageResource = imageResource;
mText1 = text1;
public int getImageResource()
return mImageResource;
public String getText1()
return mText1;
-
创建 XML 布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<RelativeLayout
android:layout_
android:layout_
android:layout_margin="4dp">
<ImageView
android:id="@+id/imageView"
android:layout_
android:layout_
android:padding="2dp" />
<TextView
android:id="@+id/textView"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/imageView"
android:text="Line 1"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
-
创建 Recycler View Adapter
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder
public ImageView mImageView;
public TextView mTextView1;
public ExampleViewHolder(View itemView)
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
public ExampleAdapter(ArrayList<ExampleItem> exampleList)
mExampleList = exampleList;
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position)
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
@Override
public int getItemCount()
return mExampleList.size();
-
你的activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context="com.example.application.recyclerviewproject.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_
android:layout_
android:background="@android:color/darker_gray"
android:padding="4dp"
android:scrollbars="vertical" />
</RelativeLayout>
-
MainActivity.java
public class MainActivity extends AppCompatActivity
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<ExampleItem> exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.img01, "Red"));
exampleList.add(new ExampleItem(R.drawable.img02, "Orange"));
exampleList.add(new ExampleItem(R.drawable.img03, "Yellow"));
exampleList.add(new ExampleItem(R.drawable.img04, "Green"));
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
【讨论】:
以上是关于ListView CustomAdapter 未显示第一个位置结果的主要内容,如果未能解决你的问题,请参考以下文章
调用 notifyDataSetChanged 时未更新带有 customAdapter 的 ListView?
使用CustomAdapter的ListView的NullPointerException [重复]
带有CustomAdapter的Listview项目上的上下文菜单android不显示