java - Android 中的微调器

标签 java android spinner android-spinner

嗨,我需要在 android 中开发一个旋转器示例。这里我使用了下面的代码:

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

这里将以下列表添加到微调框中:

 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");

这里的状态是从以前的 Activity 值获取的。其他列表是默认的 Q、P、F、I 和 C。因为第一个列表仅显示以前的状态。所以我只需要先在这里添加状态。

现在我的问题是,

现在我之前的状态是 C 意味着我的微调框必须显示以下格式:

enter image description here

此处仅显示第一个和最后一个值 C。所以 C 值在我的微调盒上有两次可用。 这是我的问题。这里我想首先添加以前的状态,同时以前的状态不添加到其他地方。在这里我该如何发展。请给我一些解决方案。例如;

如果我之前的状态是 P,那么显示的第一个值是 P,并且还会显示 Q、P、F、I、C。所以这里P显示了2次。那么我该如何解决这个错误。

编辑:

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

这里我收到以下错误: 该类型的方法 length() 未定义 列表

如何解决此错误。请帮助我

最佳答案

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

这个ifif(位置>0) 列表.removeAt(位置); 允许如果状态在您的列表中,则不会将其删除。它仍然会位于列表的顶部。

关于java - Android 中的微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12892782/

相关文章:

Java:GUI - JButton 打开新的 JPanel 并读取 JTextFields

android - 在 cordova 3.3 中执行 cordova 平台添加 android 时出错

android - jquery 移动应用程序白色补丁显示在 android 中的方向更改

android - 动态创建微调器并从 SQLite 设置其值

android - android arrayadapter的settype

java - 需要存储带有日期的 double 。我正在考虑数组但不确定

Java连接FTP服务器并使用java默认库下载文件

java - 为什么通过 newCachedThreadPool 创建的 ExecutorService 是邪恶的?

android - 使用电话号码和密码的 Firebase Auth

android - 如何在小部件右侧创建没有向下三角形的android微调器