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

You can use following approach.

<string-array name="title_values">
        <item>alltypes</item>
        <item>featurefilm</item>
        <item>tvseries</item>
        <item>videogame</item>
</string-array>

Modification in the code will be

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

            }
        });

第二个选项。

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