spinner in android

别等时光非礼了梦想. 提交于 2019-12-13 03:53:38

问题


Hi I need to develop a spinner example in android. Here I have used below code:

 public class InsertionExample extends Activity {
 private final String NAMESPACE = "http://xcart.com";
private final String URL = "http://192.168.1.168:8089/XcartLogin/services/update?wsdl";
private final String SOAP_ACTION = "http://xcart.com/insertData";
private final String METHOD_NAME = "insertData";
Button btninsert;
String selectedItem;

static final String KEY_NAME = "orderid";
static final String KEY_STATUS = "status";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.change_status);
    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    btninsert = (Button)findViewById(R.id.btn_insert1);
    btninsert.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             Intent in = getIntent();
             String orderid = in.getStringExtra(KEY_NAME);
             String status = in.getStringExtra(KEY_STATUS);
 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            PropertyInfo unameProp =new PropertyInfo();
            unameProp.setName("Status");//Define the variable name in the web service method
            unameProp.setValue(selectedItem);//Define value for fname variable
            unameProp.setType(String.class);//Define the type of the variable

            request.addProperty(unameProp);
            PropertyInfo idProp =new PropertyInfo();
            idProp.setName("Orderid");//Define the variable name in the web service method
            idProp.setValue(orderid);//Define value for fname variable
            idProp.setType(String.class);//Define the type of the variable
            request.addProperty(idProp);



              SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
              envelope.setOutputSoapObject(request);
              HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

              try{
               androidHttpTransport.call(SOAP_ACTION, envelope);
                  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

                 TextView result = (TextView) findViewById(R.id.textView2);
                  result.setText(response.toString());
             }
           catch(Exception e){

           }
              }
    });

    //attach the listener to the spinner
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    //Dynamically generate a spinner data 
    createSpinnerDropDown();

}

//Add animals into spinner dynamically
private void createSpinnerDropDown() {

    //get reference to the spinner from the XML layout
    Spinner spinner = (Spinner) findViewById(R.id.spinner1);

    //Array list of animals to display in the spinner
    List<String> list = new ArrayList<String>();
    Intent in = getIntent();

    String status = in.getStringExtra(KEY_STATUS);
    list.add(status);
    list.add("Q");
    list.add("P");
    list.add("F");
    list.add("I");
    list.add("C");

    //create an ArrayAdaptar from the String Array
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    //set the view for the Drop down list
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //set the ArrayAdapter to the spinner
    spinner.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    //attach the listener to the spinner
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

}

   public class MyOnItemSelectedListener implements OnItemSelectedListener {

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

         selectedItem = parent.getItemAtPosition(pos).toString();

       }


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

    }



    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Do nothing.
    }
}

Here the following List is added on spinner box:

 List<String> list = new ArrayList<String>();
    Intent in = getIntent();

    String status = in.getStringExtra(KEY_STATUS);
    list.add(status);
    list.add("Q");
    list.add("P");
    list.add("F");
    list.add("I");
    list.add("C");

Here the status is getting from previous activity value.other list is default Q,P,F,I and C.because the first list is displayed previous status only. So only I have to added status first here.

Now my problem is,

Now my previous status is C means my spinner box have to displayed following format:

Here the first and last values are displayed C only. So the C value is available twice on my spinner box. This is my problem. Here I want to add the previous status first and also same time the previous status is not added another place. How can I develop here. Please give me some solutions. For e.g;

If my previous status is P, then the first value displayed is P and also shows Q,P,F,I,C. So here P is displayed 2 times. So how can I resolve this error.

EDIT:

list.add(status);
    list.add("Q");
    list.add("P");
    list.add("F");
    list.add("I");
    list.add("C");
    for(i=0;i<list.length();i++){
        if(list.get(i).equals(status)) {
                list.remove(list.get(i));
        }
   }

Here I get the below error: The method length() is undefined for the type List

How can I resolve this error. Please help me


回答1:


list.add(status);
list.add("Q");
list.add("P");
list.add("F");
list.add("I");
list.add("C");
int position = -1;
    for(i=0;i<list.size();i++){
        if(list.get(i).equals(status)) {
                position = i;
        }
   }
if(position>0)
   list.removeAt(position);

This ifif(position>0) list.removeAt(position); grants that if status is in your list, it will not be removed. It will stille be at the top of the list.




回答2:


use this condition after added list:

for(i=1;i<list.size();i++){
        if(list.get(i).equals(status)) {

                list.remove(i);
        }
 }


来源:https://stackoverflow.com/questions/12892782/spinner-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!