Hi,
     I m calling 1st spinner values from strings.xml and OnItemselect i m
using HttpPost i m retreving xml data from my server for my second spinner
and on selct 1st spinner value i want to display 2nd spinner value.Here is
the code

public class HelloSpinner extends Activity {

    public String response;
    String[] name = {"","",""};
    //String[] name = new String[10];
    Spinner spinner1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //name[0]="";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        //spinner1 = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(
                this, R.array.planets_array,
android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener()

        {
            public void onItemSelected(AdapterView<?> parent,
                    View view, int pos, long id) {

                String selectedItem = (String) parent.getSelectedItem();

                System.out.println("selectedItem :"+selectedItem);

                ArrayList<NameValuePair> postParameters = new
ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("spinner",
selectedItem));

                try{
                    response = CustomHttpClient1.executeHttpPost("
http://10.163.14.75:8083/exist/rest/db/Android/test3.xq";, postParameters);
                    System.out.println("response :"+response);
                    parseByDOM(response);

                }
                catch (Exception e){
                     e.printStackTrace();

System.out.println("Exception==>"+e.getMessage());

                }

                spinner1 = (Spinner) findViewById(R.id.spinner1);

                // TODO Auto-generated method stub
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }

        });

    }

    void parseByDOM(String response)   {//response is ur xml as string
        InputStream is =null;
         int i=0;

         try {
             is = new ByteArrayInputStream(response.getBytes("UTF-8"));
         } catch (UnsupportedEncodingException e) {
             System.out.println("Encoder exception in getxml:" +
e.getMessage());
         }

        try {
            DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(is);
            doc.getDocumentElement().normalize();
            NodeList nodeLst = doc.getElementsByTagName("name");
            int totalPersons = nodeLst.getLength();
            System.out.println("totalPersons: " + totalPersons);
            for(int k=0;k<totalPersons;k++){
            Element firstNameElement = (Element)nodeLst.item(k);

            NodeList textFNList = firstNameElement.getChildNodes();
            name[i] = ((Node)textFNList.item(0)).getNodeValue().trim();
            System.out.println("First Name : " +
((Node)textFNList.item(0)).getNodeValue().trim());
            i++;
            }

            System.out.println("i value :" + i + " "+ name[0]+ " "+
name[1]+  name[2]);
        } catch (Exception e) {
            System.out.println("Xmlread error :" + e.getMessage());
        }


        ArrayAdapter<String> adapter1 = new
ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, name);

adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
             public void onItemSelected(AdapterView<?> parent,
                     View view, int pos, long id) {
                  System.out.println("name values :" + name[0] + name[1] +
name[2]);

                 Toast.makeText(parent.getContext(), "The pm is " +
                          parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
             }

             public void onNothingSelected(AdapterView<?> arg0) {
                 // TODO Auto-generated method stub
             }

        });
       }
}

and my strings.xml looks like :

<resources>
    <string name="planet_prompt">Choose a planet</string>
    <string-array name="planets_array">
        <item>Tamilnadu</item>
        <item>AndhraPradesh</item>
        <item>Karnataka</item>
        <item>Kerala</item>
    </string-array>
</resources>

and the data received from server is

<cities>
    <name stateID="01" cityID="01">Chennai</name>
    <name stateID="01" cityID="02">Trichy</name>
    <name stateID="01" cityID="03">Madurai</name>
    <name stateID="02" cityID="04">Hyderabad</name>
    <name stateID="02" cityID="05">Tirupati</name>
    <name stateID="03" cityID="06">Bangalore</name>
    <name stateID="03" cityID="07">Mangalore</name>
    <name stateID="04" cityID="08">Ernakulam</name>
    <name stateID="04" cityID="09">Palakad</name>
</cities>

On Mon, Mar 19, 2012 at 11:46 AM, Zsolt Vasvari <[email protected]> wrote:

> What have you done so far to accomplish your goals and where are you stuck?
>
>
> On Monday, March 19, 2012 1:38:37 PM UTC+8, vivek elangovan wrote:
>>
>> Hi members,
>>                     I want to display the second spinner if i only select
>> some values in my first spinner and also i want to create the spinner in my
>> class directly without declaring in my main.xml is it possible?
>>
>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
thanks & regards,
Vivek . E
9841841099

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to