我目前正在开发我的第一个Android应用程序,而且我遇到了一个奇怪的问题。好吧,我试图用TextView和ImageView创建一个自定义微调器,代码显然是正确的,但当我点击微调器选择项目应用程序粉碎...我尝试了很多代码和方法,但..仍然是同一个问题,当我检查Android监视器时,我发现此错误 E / ArrayAdapter:您必须为TextView提供资源ID ...
CustomAdapter.java:
this.UpdateAsync();
custom_spinner_row.xml
public class CustomAdapter extends ArrayAdapter {
String[] spinnerTitles;
int[] spinnerImages;
String[] spinnerPopulation;
Context mContext;
public CustomAdapter(@NonNull Context context, @LayoutRes int resource) {
super(context, resource);
}
public CustomAdapter(@NonNull Context context, String[] titles, int[] images, String[] population) {
super(context, R.layout.custom_spinner_row);
this.spinnerTitles = titles;
this.spinnerImages = images;
this.spinnerPopulation = population;
this.mContext = context;
}
@Override
public int getCount() {
return spinnerTitles.length;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder mViewHolder = new ViewHolder();
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.custom_spinner_row, parent, false);
mViewHolder.mFlag = (ImageView) convertView.findViewById(R.id.ivFlag);
mViewHolder.mName = (TextView) convertView.findViewById(R.id.tvName);
mViewHolder.mPopulation = (TextView) convertView.findViewById(R.id.tvPopulation);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mFlag.setImageResource(spinnerImages[position]);
mViewHolder.mName.setText(spinnerTitles[position]);
mViewHolder.mPopulation.setText(spinnerPopulation[position]);
return convertView;
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return super.getDropDownView(position, convertView, parent);
}
private static class ViewHolder {
ImageView mFlag;
TextView mName;
TextView mPopulation;
}

MainActivity
?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/ivFlag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tvPopulation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvName"
android:layout_toEndOf="@+id/ivFlag"
android:layout_toRightOf="@+id/ivFlag"
android:padding="5dp"
android:text="TextView"
android:textStyle="bold" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/ivFlag"
android:layout_toRightOf="@+id/ivFlag"
android:padding="5dp"
android:text="TextView"
android:textColor="@color/colorAccent"
android:textSize="20sp"
android:textStyle="italic" />
</RelativeLayout>
答案 0 :(得分:0)
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = mInflater.inflate(R.layout.custom_spinner_row, parent, false);
ImageView imageView = (ImageView) convertView.findViewById(R.id.ivFlag);
TextView nameTextView = (TextView) convertView.findViewById(R.id.tvName);
TextView populationTextView = (TextView) convertView.findViewById(R.id.tvPopulation);
imageView.setImageResource(spinnerImages[position]);
nameTextView.setText(spinnerTitles[position]);
populationTextView.setText(spinnerPopulation[position]);
return view;
}