我有一个包含5个微调器的选项页面。当用户登录并修改他们的帐户选项时,我希望微调器加载用户对微调器的选择。目前,每个微调器都在其适配器“请选择”中加载第一个值。如果检查服务器并且用户还没有做出选择那就没关系 - 但我希望根据明显的条件来决定。
这样做有好办法吗?我刚刚完成了将微调器选项发送到服务器并将它们与适当的用户一起存储的代码。所以我知道我需要不同的PHP查询,这没问题。 但是如何在onCreate里面设置微调器的可见值?
ageSpinner = (Spinner) findViewById(R.id.ageSpinner);
ageAdapter = ArrayAdapter.createFromResource
(this, R.array.ageArray, android.R.layout.simple_spinner_item);
ageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ageSpinner.setAdapter(ageAdapter);
ageSpinner.setTag("age");
ageSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
eduSpinner = (Spinner) findViewById(R.id.eduSpinner);
eduAdapter = ArrayAdapter.createFromResource
(this, R.array.eduArray, android.R.layout.simple_spinner_item);
eduAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
eduSpinner.setTag("edu");
eduSpinner.setAdapter(eduAdapter);
eduSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
maritalSpinner = (Spinner) findViewById(R.id.maritalSpinner);
maritalAdapter = ArrayAdapter.createFromResource
(this, R.array.maritalArray, android.R.layout.simple_spinner_item);
maritalAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
maritalSpinner.setTag("marital");
maritalSpinner.setAdapter(maritalAdapter);
maritalSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
nationalitySpinner = (Spinner) findViewById(R.id.nationalitySpinner);
nationalityAdapter = ArrayAdapter.createFromResource
(this, R.array.nationalityArray, android.R.layout.simple_spinner_item);
nationalityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
nationalitySpinner.setTag("nationality");
nationalitySpinner.setAdapter(nationalityAdapter);
nationalitySpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if (pos != 0) {
param.add(new BasicNameValuePair(parent.getTag().toString(),
parent.getItemAtPosition(pos).toString()));
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
答案 0 :(得分:1)
你应该创建一个扩展BaseAdapter
的类,使用带有列表的构造函数(字符串列表)并使其实现SpinnerAdapter
:
public class MySpinnerAdapter extends BaseAdapter<String> implements SpinnerAdapter {
public MySpinnerAdapter(Context context, List<String> listStrings ) {
super( context, android.R.layout.android.R.layout.simple_list_item_1, listStrings );
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if( convertView == null ) {
convertView = new TextView( getContext() );
}
((TextView) convertView).setText( getItem( position) );
}
}
答案 1 :(得分:0)
我使用链接到CustomDialog的EditTexts来显示数据。它效果更好。我有AsyncTasks将数据加载到不可编辑的EditTexts onCreate和onSubmit。这是一些相关的代码:
ageEditText = (EditText) findViewById(R.id.ageEditText);
ageEditText.setOnClickListener(fieldListener);
ageEditText.setId(0);
educationEditText = (EditText) findViewById(R.id.educationEditText);
educationEditText.setOnClickListener(fieldListener);
educationEditText.setId(1);
maritalEditText = (EditText) findViewById(R.id.maritalEditText);
maritalEditText.setOnClickListener(fieldListener);
maritalEditText.setId(2);
backgroundEditText = (EditText) findViewById(R.id.backgroundEditText);
backgroundEditText.setOnClickListener(fieldListener);
backgroundEditText.setId(3);
incomeEditText = (EditText) findViewById(R.id.incomeEditText);
incomeEditText.setOnClickListener(fieldListener);
incomeEditText.setId(4);
partyEditText = (EditText) findViewById(R.id.partyEditText);
partyEditText.setOnClickListener(fieldListener);
partyEditText.setId(5);
public OnClickListener fieldListener = new OnClickListener(){ public void onClick(查看v){ FragmentManager fm = getSupportFragmentManager(); switch(v.getId()){ case(0):
ChooseDialog ageDialog = new ChooseDialog("Age", ageEditText, act);
ageDialog.show(fm, "fragment_edit_name");
break;
case (1) :
ChooseDialog educationDialog = new ChooseDialog("Education", educationEditText, act);
educationDialog.show(fm, "fragment_edit_name");
break;
case (2) :
ChooseDialog maritalDialog = new ChooseDialog("Marital", maritalEditText, act);
maritalDialog.show(fm, "fragment_edit_name");
break;
case (3) :
ChooseDialog backgroundDialog = new ChooseDialog("Background", backgroundEditText, act);
backgroundDialog.show(fm, "fragment_edit_name");
break;
case (4):
ChooseDialog incomeDialog = new ChooseDialog("Income", incomeEditText, act);
incomeDialog.show(fm, "fragment_edit_name");
break;
case (5):
ChooseDialog partyDialog = new ChooseDialog("Income", partyEditText, act);
partyDialog.show(fm, "fragment_edit_name");
break;
}
}
};
private void submit() {
params = new ArrayList<NameValuePair>();
getButtons();
params.add(new BasicNameValuePair("age", ageEditText.getText().toString()));
params.add(new BasicNameValuePair("marital", maritalEditText.getText().toString()));
params.add(new BasicNameValuePair("background", backgroundEditText.getText().toString()));
params.add(new BasicNameValuePair("education", educationEditText.getText().toString()));
params.add(new BasicNameValuePair("income", incomeEditText.getText().toString()));
params.add(new BasicNameValuePair("sex", sexString));
params.add(new BasicNameValuePair("employment", empString));
params.add(new BasicNameValuePair("party", partyEditText.getText().toString()));
params.add(new BasicNameValuePair("user", dbHandler.getUserDetails().get("email").toString()));
DemoTask demoTask = new DemoTask(act, params);
demoTask.execute();
}