スピナーに指定された値以外の異なる値を送信させる方法:
たとえば。私の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プログラミングにかなり慣れていません。それを行う方法について何か提案はありますか?