使用Spinner调用方法?

时间:2015-11-30 09:23:11

标签: android spinner

我尝试使用2个不同的微调器来调用方法,当我使用第一个Spinner调用时它正在工作,但是当我使用其他微调器时它没有调用,而当它使用时它正在复制来自第一个微调器的数据。如何独立使用Spinners调用该方法。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:background="#FFFFFF"
        android:layout_weight="1" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
         android:background="#FFFFFF"
        android:layout_weight="1" />
</LinearLayout>

    Spinner SpinnerExample = (Spinner) findViewById(R.id.spinner);
    Spinner SpinnerExample2 = (Spinner) findViewById(R.id.spinner2);

    BTNsave = (Button) findViewById(R.id.save);

    setListData();

    Resources res = getResources();
    adapter = new CustomAdapter2(activity, R.layout.spinner_rows,
            CustomListViewValuesArr, res);
    adapter1 = new CustomAdapter3(activity, R.layout.spinner_rows,
            CustomListViewValuesArr, res);

    SpinnerExample.setAdapter(adapter);
    SpinnerExample2.setAdapter(adapter1);


    SpinnerExample.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
            // your code here
            new GetProductDetails().execute();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    });

    SpinnerExample2.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View v1, int position, long id1) {
            // your code here
            new GetProductDetails().execute();
        }
        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here

        }

    });

/**
 * Background Async Task to Get complete product details
 * */
class GetProductDetails extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Concurency_main.this);
        pDialog.setMessage("Loading details. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    /**
     * Getting User details in background thread
     * */
    protected String doInBackground(String... params) {

        String Company = ((TextView) findViewById(R.id.company)).getText()
                .toString();
        // updating UI from Background Thread
        // Check for success tag
        int success;
        try {
            List<NameValuePair> params1 = new ArrayList<NameValuePair>();
            params1.add(new BasicNameValuePair("curr", Company));

            // getting User details by making HTTP request
            // Note that User details url will use GET request
            JSONObject json = jsonParser.makeHttpRequest(
                    url_product_detials, "GET", params1);

            // check your log for json response
            Log.d("Single User Details", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                // successfully received User details
                JSONArray UserObj = json
                        .getJSONArray(TAG_PRODUCT); // JSON Array

                // get first User object from JSON Array
                final JSONObject product = UserObj.getJSONObject(0);
                runOnUiThread(new Runnable() {
                    public void run() {
                        // User with this pid found
                        // Edit Text
                            txtName = (EditText) findViewById(R.id.txtvalue);
                        // display User data in EditText
                        try {
                            txtName.setText(product.getString(TAG_VALUE));
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });

            }else{
                // User with pid not found
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once got all details
        pDialog.dismiss();
    }
}

1 个答案:

答案 0 :(得分:0)

这是因为你正在后台进行UI工作,你应该使用asynteask的onpostexecute方法来设置和解析数据或者使用RunonUIthread