如何在微调器中添加默认值,如selectone?我想在spinner中添加一个在spinne上可见的值,但是当我点击微调器时,适配器的所有值都显示在我面前,而不是selectone,这是默认值。
try {
json = new JSONObject(Status);
// nameArray = json.names();
valArray = json.getJSONArray("Machines");
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for (int i = 0; i < valArray.length(); i++) {
try {
String machineName = valArray.getJSONObject(i)
.getString("MachineName").toString();
// Integer machineID = Integer.parseInt(valArray.getJSONObject(
// 0).getString("MachineID"));
// Entities.machineID = machineID;
adapter.add(machineName);
} catch (JSONException e) {
e.printStackTrace();
}
}
sp_Machine.setAdapter(adapter);
提前致谢。
答案 0 :(得分:3)
如果要动态获取微调器的值,那么这是在微调器值的第一个处添加select的方法:
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/android/classname_spinner.php");
try{
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost,responseHandler);
JSONObject json = new JSONObject(responseBody);
JSONArray jArray = json.getJSONArray("output");
arr = new String[jArray.length()+1];
arr[0] = "-select-";
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
String sclass = json_data.getString("spinner");
arr[i+1] = sclass;
}
}catch (Exception e) {
Log.e("log_tag","Error parsing classname data"+e.toString());
}
}catch (Exception e) {
Log.e("log_tag","Request failed"+e.toString());
}
现在将您获得的值添加到微调器中,如下所示:
classSpinner = (Spinner) findViewById(R.id.editClass);
ArrayAdapter<String> classNameAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,arr);
classNameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
classSpinner.setAdapter(classNameAdapter);
现在,当想要微调器在单击事件时返回其默认值时,只需在on click事件中写入:
reset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
classSpinner.setSelection(0);
}
});
希望这会对你有所帮助。
答案 1 :(得分:0)
这是我想出的一个解决方案。只需使用此适配器而不是常规适配器:
package perelman.yuval.carsapp;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class SpinnerDualAdapter<T> extends ArrayAdapter<T> {
private ArrayAdapter<String> myAdapter;
private boolean isPromptAdapter;
private SpinnerDualAdapter(Context context, int textViewResourceId,
List<T> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
public SpinnerDualAdapter(Context context,
int textViewResourceIdForDropdownView,
int textViewResourceIdForDefaultView, List<T> objects,
String defaultValue) {
this(context, textViewResourceIdForDropdownView, objects);
ArrayList<String> myArrayList = new ArrayList<String>();
myArrayList.add(defaultValue);
myAdapter = new ArrayAdapter<String>(context,
textViewResourceIdForDefaultView, myArrayList);
isPromptAdapter=true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(isPromptAdapter){
isPromptAdapter=false;
return myAdapter.getView(position, convertView, parent);
}
TextView tv=new TextView(getContext());
tv.setText(this.getItem(position).toString());
return tv;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
isPromptAdapter=false;
return(super.getDropDownView(position, convertView, parent));
}
}