1

how can i make a drop down list on android ?i google it and found many results but from the official android website and it is forbidden

i found something called List View

how can i use it (if it was the right component i have to build)

i will add these lines because stackoverflow doesn't allow me to submit the question

<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;
?>

after adding code

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;
        }

    }
Totti
  • 665
  • 5
  • 12
  • 26
  • 3
    DropDown and Android ListView are completely different. I think you need to use Spinner (DropDown known as in Android). Here are some links which can help you http://developer.android.com/guide/topics/ui/controls/spinner.html and http://www.mkyong.com/android/android-spinner-drop-down-list-example/ – Pankaj Kumar Jun 29 '12 at 04:54
  • @PankajKumar is Spinner represents one list ? – Totti Jun 29 '12 at 04:59
  • 1
    Yes, it is same as DropDownlist. And if you want to show more than one list as Spinner items, you can add more than one list as item of Spinner. – Pankaj Kumar Jun 29 '12 at 05:10
  • @PankajKumar thank you , the example was very helpfully ,but now i am having a problem: the items of the list is selected static on the res/values/strings.xml , i want to make that dynamiclly , because i will ask the server and the server will send me data , then i put that data in the list, would you help me please and thank you very much – Totti Jun 29 '12 at 05:33
  • Make MyData class as separate class (also remove it from getCellsFromServer) and check. – Pankaj Kumar Jun 29 '12 at 07:12
  • @PankajKumar now i got this exception `The constructor ArrayAdapter(AnswerQuestion.getCellsFromServer, int, MyData[]) is undefined` – Totti Jun 29 '12 at 07:23
  • @PankajKumar when i put the code in the activity it works, – Totti Jun 29 '12 at 07:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13207/discussion-between-pankaj-kumar-and-totti) – Pankaj Kumar Jun 29 '12 at 07:32
  • this answer looks correct to me, try it out: http://stackoverflow.com/a/17650125/2027232 – string.Empty Jul 15 '13 at 08:55

1 Answers1

5

I have an example where I used Constants, hope this will help you

Constants

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

Setup of 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);

Hope this will clear your question.


More explained example

Activity code

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;
    }
}

*layout as *

<?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>

And here is SO how to add items to the spinner dynamically in android?

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • please what is MyData, i can't find this class – Totti Jun 29 '12 at 06:43
  • @Totti MyData is an inner class. You can make it as separate class also. Find this class inside SpinnerTest clss. – Pankaj Kumar Jun 29 '12 at 06:47
  • sorry i didn't see it , now i got this exception on`ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, items);` and it is `The constructor ArrayAdapter(AnswerQuestion.getCellsFromServer, int, AnswerQuestion.getCellsFromServer.MyData[]) is undefined` – Totti Jun 29 '12 at 06:51
  • maybe because `this` referes to activity, but i am working on `AsyncTask` ? and if so , how can i repair it – Totti Jun 29 '12 at 06:53
  • Can you share your code? There is no need to do any extra effort. Just copy MyData class and make it as separate class. And you can use it in AsyncTask also, but I need to see your code. – Pankaj Kumar Jun 29 '12 at 07:01
  • i really thank you for helping, i edit the question with the code and thank you very much – Totti Jun 29 '12 at 07:06