如何使微调器发送除指定值之外的其他值:
例如。我的strings.xml有以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string name="app_name">My First App</string>
<string name="edit_message">Search the Movie Title</string>
<string name="button_send">Search</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string-array name="title_type">
<item>All Types</item>
<item>Feature Film</item>
<item>TV Series</item>
<item>Video Game</item>
</string-array>
</resources>
现在,当用户点击所有类型而不是所有类型时,我需要发送所有类型。原因是我需要构建一个接受“alltypes”来获取数据的URL。
我的MainActivity.java代码为:
public class MainActivity extends Activity {
private Button btn;
private EditText edit;
private Spinner spinner;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
edit = (EditText) findViewById(R.id.edit_message);
spinner = (Spinner) findViewById(R.id.spinner);
textView = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
//Log.v("EditText", edit.getText().toString());
//Log.v("Spinner", spinner.getSelectedItem().toString());
//if(spinner.getSelectedItem().toString()=="All Types")
String url="http://cs-server.xxx.edu:23405/examples/servlet/HelloWorldExample?title="+edit.getText().toString()+"&type="+spinner.getSelectedItem().toString();
Log.v("url",url);
}
});
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.title_type,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//String url="http://cs-server.xxx.edu:23405/examples/servlet/HelloWorldExample?tit le="+edit.getText().toString()+"&type="+spinner.getSelectedItem().toString();
// Log.v("url",url);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
textView.setText(result);
}
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://facebook.com"});
}
}
我对Android编程很新。 有关如何做的任何建议吗?
答案 0 :(得分:1)
yourspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
String selecteditem = yourspinner.getSelectedItem().toString();
selecteditem = selecteditem.equals("All Types")?selecteditem..replaceAll(" ", "").toLowerCase():selecteditem;
// removes the space and converts to lowercase
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
第二选项。
String[] customarr = {"feature,tv_series,game","feature","tv_series","game"};
String selecteditem = customarr[yourspinner.getSelectedItemPosition()]
答案 1 :(得分:1)
您可以使用以下方法。
<string-array name="title_values">
<item>alltypes</item>
<item>featurefilm</item>
<item>tvseries</item>
<item>videogame</item>
</string-array>
代码中的修改将是
String[] titleValues = getResources().getStringArray(
R.array.title_values);
titleSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView <? > adapterView, View view,
int position, long id) {
String selectedItem = titleValues[position];
}
public void onNothingSelected(AdapterView <? > arg0) {
}
});