我创建了一个带有自定义适配器的微调器,其中我有一个图像视图和一个文本视图,当我从微调器中选择项目然后我显示图像与所选项目工作完全正常。
问题:
但问题是,当我选择任何项目时,它会在下拉列表中显示所选项目的图像。并在spinner中显示相同的(图像和文本)..
我想要什么
中只显示没有图片的微调器中的文字图像它在旋转器中显示带有文本的图像...我不想要这个
以下是我正在使用的代码,请向我建议解决方案谢谢
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
<?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>
答案 0 :(得分:3)
你有两种方法
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;
}
答案 1 :(得分:1)
您需要为getDropDownView
和getView
方法分隔观看次数。对于用于选定值的行,将调用getDropDownView
。另一方面,当您打开微调器时,会为每一行调用getView
。
答案 2 :(得分:0)
试试这个,
file = File.new('result.txt', 'w+')
# If you prefer `open`, that works too!
# file = File.open('result.txt', 'w+')
path = Dir[ENV['HOME'] + '/Desktop/Test/*.txt']
file.puts "this is a #{path} test: "
# ensure this is closed, or you'll have memory issues if you do this often
file.close