gpt4 book ai didi

android - Spinner 在 android 中隐藏下拉 View 的第一项

转载 作者:行者123 更新时间:2023-11-30 02:50:42 31 4
gpt4 key购买 nike

I have a spinner in that i have custom adapter to add image and text when i click the spinner the array of 1st item should he hide. i have tried below code its working the first item has been gone, but remaning item has invisble mode , how to solve this

这是我的代码

private String[] accident_type_content = { "Type Of emergency","Road Accident", "Medical Emergency", "Crime", "Poisoning", "Fire","Drowning" };
private int accident_type_content_images[] = {R.drawable.ic_type_of_emergency, R.drawable.ic_road_accident,R.drawable.ic_medical_emergency, R.drawable.ic_crime_rape,R.drawable.ic_posion, R.drawable.ic_fire, R.drawable.ic_other };

My array adapter class

public class AccidentType extends ArrayAdapter<String> {

Holder holder = new Holder();

public AccidentType(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
}

@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater(null);
convertView = inflater.inflate(R.layout.no_accident, parent,
false);

holder.name = (TextView) convertView.findViewById(R.id.name);
holder.icon = (ImageView) convertView.findViewById(R.id.image);

if(position==0)
{
holder.icon.setVisibility(View.GONE);
holder.icon.setVisibility(View.GONE);
}
else{
holder.icon.setVisibility(View.VISIBLE);
holder.icon.setVisibility(View.VISIBLE);
}
return convertView;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView,
ViewGroup parent) {

LayoutInflater inflater = getLayoutInflater(null);
View row = inflater.inflate(R.layout.no_accident, parent,
false);

holder.name = (TextView) row.findViewById(R.id.name);
holder.icon = (ImageView) row.findViewById(R.id.image);
holder.name.setText(accident_type_content[position]);

holder.icon.setVisibility(View.VISIBLE);
holder.icon.setVisibility(View.VISIBLE);

if(position==accident_type.getSelectedItemPosition())
{
row.setBackgroundColor(getContext().getResources().getColor(R.color.spinner_selected_bg));
}


holder.icon.setImageResource(accident_type_content_images[position]);

return row;
}
private class Holder
{
TextView name;
ImageView icon;
}

}

并添加到微调器中

accident_type.setAdapter(new AccidentType(getActivity(),R.layout.accident_content, accident_type_content));

i want to hide the "type Of emergency " and ic_type_of_emergency image on spinner click

最佳答案

设置要隐藏的索引的索引(或数组列表)。

public class AccidentType extends ArrayAdapter<String> {

Holder holder = new Holder();
private int hideItemIndex;

public AccidentType(Context context, int textViewResourceId,
String[] objects, int hideItemIndex) {
super(context, textViewResourceId, objects);
this.hidingItemIndex = hidingItemIndex;
}

@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater(null);
convertView = inflater.inflate(R.layout.no_accident, parent,
false);
convertView= null;
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.icon = (ImageView) convertView.findViewById(R.id.image);

if(position==hideItemIndex)
{
holder.icon.setVisibility(View.GONE);
holder.icon.setVisibility(View.GONE);
convertView= holder;
}
else{
convertView= super.getDropDownView(position, null, parent);
}
return convertView;

}

创建项目列表。

int hideItemIndex = 0;

CustomAdapter dataAdapter = new CustomAdapter(this, android.R.layout.simple_spinner_item, list, hideItemIndex);

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(dataAdapter);

我想这可能对你有帮助。

关于android - Spinner 在 android 中隐藏下拉 View 的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24241611/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com