我有一个微调器,我当前正在显示静态数据,如何显示动态数据(例如:来自字符串数组)或类似适配器,以便当我从服务器接收动态值时,我可以直接输入旋转器
以下是我的代码:
if(value.contains("Extra")){
spinner.setVisibility(View.VISIBLE);
image.setVisibility(View.GONE);
triangleimage.setVisibility(View.VISIBLE);
spinner.setOnItemSelectedListener(Adapter.this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (position == getCount()) {
((TextView)v.findViewById(android.R.id.text1)).setText("");
((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
}
return v;
}
@Override
public int getCount() {
return super.getCount()-1; // you don't display last item. It is used as hint.
}
};
adapter.setDropDownViewResource(R.layout.custom_spinner_list);
adapter.add("One");
adapter.add("Two");
adapter.add("Three");
adapter.add("Four");
adapter.add("Five");
adapter.add("Six");
adapter.add(""); //Add empty string in db option at last
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getCount()); //set the hint the default selection so it appears on launch.
spinner.setOnItemSelectedListener(this);
notifyDataSetChanged();
}
答案 0 :(得分:3)
ArrayAdapter 中有一个构造函数可以将数组列表作为参数传递。请参阅下面的示例。
// Assume this list is from server
List<String> your_array_list = new ArrayList<String>();
your_array_list.add("Test");
your_array_list.add("Working");
// This is the array adapter, it takes the context of the activity as a
// first parameter, the type of list view as a second parameter and your
// array as a third parameter.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
your_array_list );
yourSpinner.setAdapter(arrayAdapter);
这应该有用。
答案 1 :(得分:1)
首先将所有数据存储在一个arraylist或数组中,然后将其传递给spinner的适配器,它的工作......
array_name.add(your_data);
答案 2 :(得分:0)