gpt4 book ai didi

java - 将微调器中的默认值设置为 "Select"

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:58 25 4
gpt4 key购买 nike

我们如何将微调器中的“选择”(默认值)设置为第一个值?我浏览过这里,没有找到任何明确的解决方案或不理解它们。我在下面粘贴我的示例代码。请根据我的理解提出一个解决方案或解释。谢谢!

这是我的主要 Activity :

int[] icons = new int[]{R.drawable.bike, R.drawable.briefcase,
R.drawable.butterfly, R.drawable.coffee, R.drawable.home, R.drawable.kids, R.drawable.monitoring};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page2);
spinner = (Spinner) findViewById(R.id.spinner1);
CustomAdapter customAdapter = new CustomAdapter(this, getResources().getStringArray(R.array.titles),
getResources().getStringArray(R.array.description), icons);
spinner.setAdapter(customAdapter);
}

这是我的适配器

public class CustomAdapter  extends ArrayAdapter<String>{

Context c;
String [] titles;
String [] desc;
int [] icons;
public CustomAdapter(Context context, String[] titles, String [] desc, int [] imgs) {
super(context, R.layout.layout_row, R.id.titles, titles);
this.c = context;
this.titles = titles;
this.desc = desc;
this.icons = imgs;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyViewHolder holder = null;
if(row == null){
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.layout_row, parent, false);
holder = new MyViewHolder(row);
row.setTag(holder);
}else {
holder = (MyViewHolder) row.getTag();
}
holder.img_Icon.setImageResource(icons[position]);
holder.tv_Titles.setText(titles[position]);
holder.tv_Desc.setText(desc[position]);
return row;
}
}

class MyViewHolder{
TextView tv_Titles;
TextView tv_Desc;
ImageView img_Icon;
public MyViewHolder(View v) {
tv_Titles = (TextView) v.findViewById(R.id.titles);
tv_Desc= (TextView) v.findViewById(R.id.desc);
img_Icon = (ImageView) v.findViewById(R.id.icons);
}
}

我的字符串数组资源:

<string-array name="titles">
<item>Sunday</item>
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thursday</item>
<item>Friday</item>
<item>Saturday</item>
</string-array>

<string-array name="description">
<item>This is Sunday</item>
<item>This is Monday</item>
<item>This is Tuesday</item>
<item>This is Wednesday</item>
<item>This is Thursday</item>
<item>This is Friday</item>
<item>This is Saturday</item>
</string-array>

最佳答案

将“Select”添加为标题数组和 CustomAdapter 中的第一项,如果行位置为 0,则仅设置 holder.tv_Titles.setText(titles[position]); 并将 older.img_Iconholder.tv_Desc 的可见性设置为 GONE。单击任何微调器项目时,检查单击的项目位置是否为 0。如果为 0,则显示错误或忽略。

关于java - 将微调器中的默认值设置为 "Select",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42350485/

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