我用自定义适配器创建了一个旋转器,其中我有一个图像视图和一个文本视图,当我从旋转器中选择项目时,我用选定的项目显示图像,这是完美的工作。
问题:
但问题是当我选择任何项目时,它会在下拉框中显示选定项目的图像。并在旋转器中显示相同的(图像和文本)…
public class SpinnerAdapter extends ArrayAdapter<SpinnerItem> {
private Context mContext;
private ArrayList<SpinnerItem> listState;
public SpinnerAdapter(Context context, int resource,
ArrayList<SpinnerItem> objects) {
super(context, resource, objects);
this.mContext = context;
this.listState = objects;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@SuppressLint("InflateParams")
public View getCustomView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item_layout,
null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckedImage = (ImageView) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
if (listState.get(position).isSelected()) {
holder.mCheckedImage.setVisibility(View.VISIBLE);
} else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
return convertView;
}
private class ViewHolder {
private TextView mTextView;
private ImageView mCheckedImage;
}
public void setSpinnerAdapter(ArrayList<SpinnerItem> spinnerItems) {
this.listState = spinnerItems;
notifyDataSetChanged();
}
}
private void setBottelCountData() {
final String[] select_qualification = { "", "1", "2",
"3" };
bottelCountList = new ArrayList<>();
for (int i = 0; i < select_qualification.length; i++) {
SpinnerItem spinnerItem = new SpinnerItem();
spinnerItem.setTitle(select_qualification[i]);
if (i == 2) {
spinnerItem.setSelected(false);
} else {
spinnerItem.setSelected(false);
}
bottelCountList.add(spinnerItem);
}
bottelCountAdapter = new SpinnerAdapter(getActivity(), 0,
bottelCountList);
bottelCountAdapter.setDropDownViewResource(R.layout.spinner_item_layout);
bottel_Count_Spinner.setAdapter(bottelCountAdapter);
bottel_Count_Spinner.setSelection(3);
}
bottel_Count_Spinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
if (position == 0) {
for (int count = 0; count < bottelCountList.size(); count++) {
bottelCountList.get(count).setSelected(false);
}
noOfBottel = 0;
return;
}
bottelCountList.get(position).setSelected(true);
for (int count = 0; count < bottelCountList.size(); count++) {
if (position != count) {
bottelCountList.get(count).setSelected(false);
}
}
bottel_Count_Spinner.setPrompt("Hello");
bottelCountAdapter.setSpinnerAdapter(bottelCountList);
noOfBottel = Integer.parseInt(bottelCountList.get(
position).getTitle());
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkbox"
android:layout_centerVertical="true"
android:padding="10dp"/>
<ImageView
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:src="@android:drawable/checkbox_on_background"
android:contentDescription="@string/app_name"
android:layout_marginLeft="10dp"
android:layout_alignParentStart="true" />
</RelativeLayout>
你有两种方法
getview
-选择值后显示getDropDownView
-定义打开下拉菜单时视图的外观您必须修改GetCustomView
并处理差异(隐藏复选框图像)。
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent, true);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent, false);
}
@SuppressLint("InflateParams")
public View getCustomView(int position, View convertView, ViewGroup parent, boolean isDropDown) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item_layout,
null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckedImage = (ImageView) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
if (isDropDown) {
if (listState.get(position).isSelected()) {
holder.mCheckedImage.setVisibility(View.VISIBLE);
} else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
}
else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
return convertView;
}
我最近开始编写android程序,我想看看有哪些语言的文本到语音可用在设备上,并显示在一个旋转器,但我遇到了一个非常奇怪的问题。 下面是代码的相关部分: 在这个TTS初始化之后,我尝试并打印sum,它显示为0。我尝试通过执行类似String=country.get(1)的操作来访问localeList和country中的元素;它会在编译器中给出一个错误,说我试图访问的索引不存在,列表中有0个元素。
最后,这是我想从中提取的字符串数组文件(我是把我的字符串数组放在它自己的文件中的values文件夹中,还是放在字符串文件中,我都看过了,不知道是哪个,谢谢) 因此,我要重述一下我的问题,我需要使用还是,我如何设置我的ArrayAdapter,以及我需要把我的string-array放在哪里? 先谢谢您!
null 请注意,我试图展示一个本地旋转器。谢了!
我在Java8u40中测试了旋转器控制 现在我可以插入数字和文本了。有没有可以作为例证的例子?
但似乎什么都没起作用。
我知道这已经被问了很多次了,但没有任何答案可以帮助我。 我有一个对话框,它将在某个时刻弹出(当我调用方法“showServerDialog”时)。对话框显示出来,一切正常,直到我使用“negativeButton”关闭对话框,然后旋转屏幕。然后,对话框再次出现,尽管我取消了它。 我读了很多次,如果我使用show Dialog(int id)方法显示对话框并覆盖onCreateDialog(int