0

スピナーに指定された値以外の異なる値を送信させる方法:

たとえば。私の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プログラミングにかなり慣れていません。それを行う方法について何か提案はありますか?

4

2 に答える 2

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) {


     }
 });
于 2012-11-28T04:04:16.587 に答える
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

            }
        });

2番目のオプション。

 String[] customarr = {"feature,tv_series,game","feature","tv_series","game"};
 String selecteditem = customarr[yourspinner.getSelectedItemPosition()]
于 2012-11-28T03:40:25.980 に答える