公共类MainActivity扩展了Activity {
String[] DayOfWeek = { "Select Item", "1", "2", "3", "4", "5", "6" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner mySpinner = (Spinner) findViewById(R.id.spinner);
mySpinner.setAdapter(new MyCustomAdapter(MainActivity.this,
R.layout.row, DayOfWeek));
}
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
// return super.getView(position, convertView, parent);
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
TextView label = (TextView) row.findViewById(R.id.weekofday);
label.setText(DayOfWeek[position]);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
if (DayOfWeek[position] == "Sunday") {
icon.setImageResource(R.drawable.icon);
} else {
icon.setImageResource(R.drawable.icongray);
}
return row;
}
}
}
这里是我的代码我能够在微调器中显示值但我想要显示仅在Textview上选择项目而不是在值中,而它的值显示也告诉我如何删除它请看屏幕并告诉我如何删除< / p>
答案 0 :(得分:1)
一个简单的解决方案是:
a)将数组修改为
String[] DayOfWeek = { "1", "2", "3", "4", "5", "6" };
b)调用mySpinner.setPromptId()并设置一个字符串reosurceId,其值为“Select item”。
仅在未打开或关闭下拉列表时才设置“选择项目”,请点击此链接How to make an Android Spinner with initial text "Select One"?
答案 1 :(得分:0)
ListPopupWindow numberList;
TextView spDays;
ArrayList<Map<String, String>>() listTrans;
in oncreate spDays.setonclicklistner(this);spDays.setText("Select");
setNumberListSpinnerView();
in onclick(){
when spDays clicked :- numberList.show();
}
void setNumberListSpinnerView() {
numberList= new ListPopupWindow(this);
numberList.setAnchorView(spDays);
numberList.setOnItemClickListener((new AdapterView.OnItemClickListener() {
@Override
getListItem();
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Map map = listTrans.get(position);
spDays.setText(map.get("circle_name").toString());
circle_name = map.get("circle_name") + "";
circle_id = map.get("circle_id").toString();
circleList.dismiss();
Log.d("Circle id:", circle_id + "");
getRetails();
}
}));
}
void getListItem(){
String[] numbers = {"1","2","3","4","5","6"};
listTrans = new ArrayList<Map<String, String>>();
LinkedHashMap<String, String> tran = new LinkedHashMap<String, String>();
for (String number : numbers) {
tran.put("numbers", number);
listTrans.add(tran);
}
SimpleAdapter adapter = new SimpleAdapter(AddRetailSurvey.this, listTrans,
android.R.layout.simple_spinner_dropdown_item,
new String[]{"numbers"},
new int[]{android.R.id.text1});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
numberList.setAdapter(adapter);
}
根据您的要求更改代码,如果您遇到任何问题,请检查并告知我们。这项技术的最佳部分是你可以改变文字颜色。谢谢:))