android创建下拉列表

时间:2012-06-29 04:50:04

标签: android android-layout

我如何在Android上制作一个下拉列表?我谷歌它并发现许多结果,但从官方的Android网站,它被禁止

我找到了一个名为List View

的东西我该如何使用它(如果它是我必须构建的正确组件)

我将添加这些行,因为stackoverflow不允许我提交问题

<tag status="remove" because="can't add question"/>
int i=0; while(i=1;i<4;i++)
system.out.println("sorry");
<?Php
$word="sorry";
echo $word;
?>
添加代码后

public class getCellsFromServer extends
            AsyncTask<String, Integer, String[]> {
        ProgressDialog dialog;

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPostExecute(String[] results) {
            super.onPostExecute(results);
            final MyData items [] = new MyData[results.length];
            for(int i=0;i<results.length;i++){
                items[i]= new MyData(results[i],results[i]);
            }
            ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(this,
                    android.R.layout.simple_spinner_item, items);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(AnswerQuestion.this);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setMax(100);
            dialog.show();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            dialog.incrementProgressBy(values[0]);
        }

        @Override
        protected String[] doInBackground(String... params) {
            for (int i = 0; i < 20; i++) {
                publishProgress(5);
                try {
                    Thread.sleep(88);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            dialog.dismiss();
            URI website;
            try {
                HttpClient client = new DefaultHttpClient();
                website = new URI(
                        "http://10.0.2.2:8080/LocalizedBasedComptitionServer/GetCells");
                HttpPost request = new HttpPost();
                request.setURI(website);
                HttpResponse response = client.execute(request);
                ObjectInputStream in = new ObjectInputStream(response.getEntity().getContent()); //Android
                String commingArray ="";
                int c=0;
                c=in.read();
                while(c!=-1){
                    commingArray+=(char)c;
                    c=in.read();
                }
                String[] ar = commingArray.split(",");
                return ar;
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        class MyData {
            public MyData(String spinnerText, String value) {
                this.spinnerText = spinnerText;
                this.value = value;
            }

            public String getSpinnerText() {
                return spinnerText;
            }

            public String getValue() {
                return value;
            }

            public String toString() {
                return spinnerText;
            }

            String spinnerText;
            String value;
        }

    }

1 个答案:

答案 0 :(得分:3)

我有一个例子,我使用常量,希望这会帮助你

常量

public static final CharSequence[] DAYS_OPTIONS  = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Spinner的设置

ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, DAYS_OPTIONS);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

希望这会清除你的问题。


更多解释示例

活动代码

public class SpinnerTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner s = (Spinner) findViewById(R.id.spinner);
        //Prepar adapter 
        //HERE YOU CAN ADD ITEMS WHICH COMES FROM SERVER.
        final MyData items[] = new MyData[3];
        items[0] = new MyData("key1", "value1");
        items[1] = new MyData("key2", "value2");
        items[2] = new MyData("key3", "value3");
        ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(this,
                android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);
        s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                MyData d = items[position];

                //Get selected value of key 
                String value = d.getValue();
                String key = d.getSpinnerText();
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    class MyData {
        public MyData(String spinnerText, String value) {
            this.spinnerText = spinnerText;
            this.value = value;
        }

        public String getSpinnerText() {
            return spinnerText;
        }

        public String getValue() {
            return value;
        }

        public String toString() {
            return spinnerText;
        }

        String spinnerText;
        String value;
    }
}

*布局为*

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <Spinner android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:prompt="@string/item_prompt"
    />


</LinearLayout>

这是SO how to add items to the spinner dynamically in android?