在我的应用程序中,我想使用CustomAdapter类创建SpinnerList,为此我写了下面的代码但是当我点击微调器列表然后数组列表图像没有加载到spinner列表中,就像我的下面屏幕为什么这个问题是未来?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Category:"
android:layout_marginBottom="5dp"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
public static final String[] titles = new String[] { "Strawberry",
"Banana", "Orange", "Hello" };
public static final Integer[] images = { R.drawable.image1,
R.drawable.image2, R.drawable.image3, R.drawable.image4 };
Spinner spinner;
List<RowItem> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(titles[i],images[i]);
rowItems.add(item);
}
spinner = (Spinner)findViewById(R.id.spinner);
CustomAdapter adapter = new CustomAdapter(MainActivity.this,
R.layout.listitems_layout, R.id.title, rowItems);
spinner.setAdapter(adapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/image1"
android:padding="10dp"
/>
<TextView
android:layout_marginTop="30dp"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/icon"
android:textColor="#CC0033"
android:text="Strawberry"
android:textSize="16dp" />
</LinearLayout>
package com.example.venkat.spinnerexample1;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class CustomAdapter extends ArrayAdapter<RowItem> {
LayoutInflater flater;
public CustomAdapter(Activity context,int resouceId, int textviewId, List<RowItem> list){
super(context,resouceId,textviewId, list);
flater = context.getLayoutInflater();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RowItem rowItem = getItem(position);
View rowview = flater.inflate(R.layout.listitems_layout,null,true);
TextView txtTitle = (TextView) rowview.findViewById(R.id.title);
txtTitle.setText(rowItem.getTitle());
ImageView imageView = (ImageView) rowview.findViewById(R.id.icon);
imageView.setImageResource(rowItem.getImageId());
return rowview;
}
}
package com.example.venkat.spinnerexample1;
public class RowItem {
private int ImageId;
private String Title;
public RowItem(String Title,int ImageId){
this.Title = Title;
this.ImageId = ImageId;
}
public String getTitle(){
return Title;
}
public void setTitle(String Title){
this.Title = Title;
}
public int getImageId(){
return ImageId;
}
public void setImageId(int ImageId){
this.ImageId = ImageId;
}
@Override
public String toString() {
return Title ;
}
}
答案 0 :(得分:23)
Change your adapter like this
public class CustomAdapter extends ArrayAdapter<RowItem> {
LayoutInflater flater;
public CustomAdapter(Activity context, int resouceId, int textviewId, List<RowItem> list){
super(context,resouceId,textviewId, list);
// flater = context.getLayoutInflater();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return rowview(convertView,position);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return rowview(convertView,position);
}
private View rowview(View convertView , int position){
RowItem rowItem = getItem(position);
viewHolder holder ;
View rowview = convertView;
if (rowview==null) {
holder = new viewHolder();
flater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowview = flater.inflate(R.layout.listitems_layout, null, false);
holder.txtTitle = (TextView) rowview.findViewById(R.id.title);
holder.imageView = (ImageView) rowview.findViewById(R.id.icon);
rowview.setTag(holder);
}else{
holder = (viewHolder) rowview.getTag();
}
holder.imageView.setImageResource(rowItem.getImageId());
holder.txtTitle.setText(rowItem.getTitle());
return rowview;
}
private class viewHolder{
TextView txtTitle;
ImageView imageView;
}
}
答案 1 :(得分:20)
在适配器构造函数中,也发送textview的id
public CustomAdapter(Activity context,int resouceId, int textviewId, List<RowItem> list){
super(context,resouceId,textviewId, list);
flater = context.getLayoutInflater();
}
通过
调用它CustomAdapter adapter = new CustomAdapter(MainActivity.this,
R.layout.listitems_layout, R.id.title, rowItems);
修改强> 您的图片未显示,因为您没有覆盖getDropdownView()方法。下拉列表可见时,此方法决定子项的布局。所以将此方法添加到您的适配器
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = flater.inflate(R.layout.list_itemslayout,parent, false);
}
RowItem rowItem = getItem(position);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(rowItem.getTitle());
ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
imageView.setImageResource(rowItem.getImageId());
return convertView;
}
<强>建议强> 在你的getView()中检查(convertview == null)。它可能对这个小型适配器没有任何影响,但如果您的适配器有更多项目,它将影响性能。
修改强> 要将微调器下拉列在锚点下方,请使用
在你的微调器内 android:overlapAnchor="false"
答案 2 :(得分:-1)
我们还可以使用ViewHolder Pattern
public class TypeAdapter extends ArrayAdapter<RowItem> {
public TypeAdapter(@NonNull Context context, ArrayList<Calendar> objects) {
super(context, R.layout.calendar_type_row, objects);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return rowView(convertView, position);
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return rowView(convertView, position);
}
class ViewHolder extends RecyclerView.ViewHolder {
//Views
public ViewHolder(@NonNull View itemView) {
super(itemView);
//Init Views by itemView.findViewById
}
}
private View rowView(View convertView, int position) {
if(convertView==null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.calendar_type_row,null);
}
RowItem rowType = getItem(position);
ViewHolder viewHolder = new ViewHolder(convertView);
//Setup Values
return convertView;
}
}