【问题标题】:parse value is save in edittext field android解析值保存在edittext字段android中
【发布时间】:2012-09-06 06:27:56
【问题描述】:

在这里,我必须成功地将前一个活动的值解析到下一个活动...现在我希望将该值保存在我的编辑文本字段中...我该如何开发它..

在我的代码中,必须从以前的活动中解析 orderid 值并显示在此活动中。这里的 orderid 值自动保存在我的编辑文本字段中...我该怎么办...

在我之前的活动代码中:

 Button btninsert = (Button) findViewById(R.id.btn_insert);
btninsert.setOnClickListener(new View.OnClickListener() {


public void onClick(View v) {
     String s= getIntent().getStringExtra("orderid");
Intent i = new Intent(getApplicationContext(), InsertionExample.class);
i.putExtra(KEY_NAME, s);
startActivity(i);
}
});

这是我的 InsertionExample.java 类:

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";
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.change_status);
    Intent in = getIntent();

    // Get XML values from previous intent
    String orderid = in.getStringExtra(KEY_NAME);

    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(R.id.textView1);


    lblName.setText(orderid);


    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    btninsert = (Button)findViewById(R.id.btn_insert1);
    btninsert.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            EditText Orderid = (EditText) findViewById(R.id.orderid);
            String orderid = Orderid.getText().toString();
        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>();

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

这里我使用下面的代码在edittext字段上设置orderid:idProp.setValue(orderid);/

            EditText Orderid = (EditText) findViewById(R.id.orderid);
            String orderid = Orderid.getText().toString();
            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);`

但我希望edittext 字段必须自动输入我解析的orderid。 (即)

Intent in = getIntent();

    // Get XML values from previous intent
    String orderid = in.getStringExtra(KEY_NAME);

    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(R.id.textView1);


    lblName.setText(orderid);

这个orderid是自动进入edittext框的。。。怎么办。。。 请帮助我...给我一些想法。

【问题讨论】:

  • 你用的是eclipse,对吧?请在发布之前按 ctrl+shift+f 并格式化你该死的代码。此外,如果它无法编译,最好说“它不会编译”,因为您似乎已经发布了它,就像您希望人们尝试或通读它一样。

标签: java android parameter-passing adapter


【解决方案1】:

想要将值发送给其他人的活动将执行此操作.....

Button bn = (Button)findViewById(R.id.bn);
bn.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        Intent intent = new Intent(PutExtra.this, getExtra.class);
        intent.putExtra("text", et.getText().toString());
        startActivity(intent);
    }
});

被调用的活动和想要的值将不得不这样做

override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.getextra);

        EditText tv = (EditText)findViewById(R.id.tv);

        Intent intent = getIntent();
        String extra = intent.getStringExtra("text");      
        tv.setText(extra);
    }

【讨论】:

  • 请阅读我更新的问题并为我提供解决方案
  • ok.. 你到底想对你的编辑文本做什么?您想将从 getStringExtra() 获得的值放入编辑文本中还是不必将该值放入编辑文本中
【解决方案2】:

不要使用 startActivity(Intent i),而是使用 startActivityForResult(Intent i, key k)。一旦你完成了你的活动(调用完成())并返回到根活动,你将在 onActivityResult() 中捕捉到“结果”。

如果您需要更多信息,请查看此链接http://android-er.blogspot.com/2011/08/return-result-to-onactivityresult.html

【讨论】:

    猜你喜欢
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多