获取微调器值取决于另一个微调器的选择

时间:2015-09-01 12:27:23

标签: java android json

我正在尝试创建一个功能,我可以根据单独的微调器的选择获取微调器值,然后将两个值发布到mysql数据库中,微调器选择工作正常,但它没有发布到数据库,它不是产生任何错误,有人可以告诉我哪里出错了吗?这是我的java。

import android.support.v7.app.ActionBarActivity;

import java.util.ArrayList;
import java.util.List;


import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class MainActivity extends Activity implements
OnItemSelectedListener{
Spinner s1,s2;
Button btnCreateProduct;

private ProgressDialog pDialog;
//url to create new product
private static String url_create_product = "http://bsa.co.ke/call/create_product.php";
JSONParser jsonParser = new JSONParser();
// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    s1 = (Spinner)findViewById(R.id.spinner1);
    s2 = (Spinner)findViewById(R.id.spinner2);
    btnCreateProduct = (Button)findViewById(R.id.btnCreateProduct);
    s1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    String sp1= String.valueOf(s1.getSelectedItem());
    Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
    if(sp1.contentEquals("Eldoret")) {
        List<String> list = new ArrayList<String>();
        list.add("Tuskys Zion");
        list.add("Tuskys Wareng");
        list.add("Naivas Referral");
        list.add("Ukwala A");
        list.add("Ukwala A");
        list.add("Naivas Sokoni");
        list.add("Uchumi Sugar Land");
        list.add("Uchumi Super");
        list.add("Transmatt");
        list.add("Nakumatt");
        list.add("Khetias");
        list.add("Eldo");
        list.add("Uchumi Sugar Land");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);`enter code here`
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter.notifyDataSetChanged();
        s2.setAdapter(dataAdapter);
    }
    if(sp1.contentEquals("Kitale")) {
        List<String> list = new ArrayList<String>();
        list.add("Suam");
        list.add("Khetias Gigamatt");
        list.add("Khetias Center Point");
        list.add("Khetias Cross Roads");
        list.add("Khetias Euro");
        list.add("Tuskys");
        list.add("Nakumatt");

        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        s2.setAdapter(dataAdapter2);
    }
    if(sp1.contentEquals("Kakamega")) {
        List<String> list = new ArrayList<String>();
        list.add("Yako");
        list.add("Nakumatt");
        list.add("Purchase");
        list.add("Mama Watoto");
        list.add("Tuskys Shilloah");
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        s2.setAdapter(dataAdapter2);
    }
    if(sp1.contentEquals("Kisii")) {
        List<String> list = new ArrayList<String>();
        list.add("Oshwal");
        list.add("Naivas");
        list.add("Tuskys Chingware");
        list.add("Uchumi");
        list.add("Nakumatt");
        list.add("Chamunda");
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
          dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        s2.setAdapter(dataAdapter2);
    }

    if(sp1.contentEquals("Kericho")) {
        List<String> list = new ArrayList<String>();
        list.add("Uchumi");
        list.add("Stagematt");
        list.add("Ukwala");
        list.add("Tuskys Ndonyo");
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        s2.setAdapter(dataAdapter2);
    }


Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);

// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        // creating new product in background thread
        new CreateNewProduct().execute();
    }
});
}

  /**
  * Background Async Task to Create new product
  * */
 class CreateNewProduct extends AsyncTask<String, String, String> {

/**
 * Before starting background thread Show Progress Dialog
 * */
@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(MainActivity.this);
    pDialog.setMessage("Creating Product..");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
}

/**
 * Creating product
 * */
protected String doInBackground(String... args) {
    String name =  s1.getSelectedItem().toString();
    String price = s2.getSelectedItem().toString();


    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("name", name));
    params.add(new BasicNameValuePair("price", price));


    // getting JSON Object
    // Note that create product url accepts POST method
    JSONObject json = jsonParser.makeHttpRequest(url_create_product,
            "POST", params);

    // check log cat fro response
    Log.d("Create Response", json.toString());

    // check for success tag
    try {
        int success = json.getInt(TAG_SUCCESS);

        if (success == 1) {
            // successfully created product
            Intent i = new Intent(getApplicationContext(),      MainActivity.class);
            startActivity(i);

            // closing this screen
            finish();
        } else {
            // failed to create product
        }
    } 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 done
    pDialog.dismiss();
}
}

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

这是我的字符串

  <resources>

    <string name="app_name">Spinner Example</string>
    <string name="action_settings">Settings</string>
    <string name="lblAcc">Select Account Type</string>
    <string name="lblSubAcc">Select Account Head</string>

  <string-array name="acc_type">
        <item>Eldoret</item>
        <item>Kitale</item>
        <item>Kakamega</item>
        <item>Kisii</item>
        <item>Kericho</item>
    </string-array>

  </resources>

1 个答案:

答案 0 :(得分:0)

您必须以这种方式获得选定的微调器值:

String sp1= s1.getSelectedItem().toString();

而不是

String sp1= String.valueOf(s1.getSelectedItem());

您已使用contentEquals()方法比较字符串。您需要使用equals()equalsIgnoreCase()来比较字符串。

尝试使用此方法而不是您的方法。

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    String sp1= s1.getSelectedItem().toString();
    Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
    if(sp1.equalsIgnoreCase("Eldoret")) {
        List<String> list = new ArrayList<String>();
        list.add("Tuskys Zion");
        list.add("Tuskys Wareng");
        list.add("Naivas Referral");
        list.add("Ukwala A");
        list.add("Ukwala A");
        list.add("Naivas Sokoni");
        list.add("Uchumi Sugar Land");
        list.add("Uchumi Super");
        list.add("Transmatt");
        list.add("Nakumatt");
        list.add("Khetias");
        list.add("Eldo");
        list.add("Uchumi Sugar Land");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);`enter code here`
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter.notifyDataSetChanged();
        s2.setAdapter(dataAdapter);
    }
    if(sp1.equalsIgnoreCase("Kitale")) {
        List<String> list = new ArrayList<String>();
        list.add("Suam");
        list.add("Khetias Gigamatt");
        list.add("Khetias Center Point");
        list.add("Khetias Cross Roads");
        list.add("Khetias Euro");
        list.add("Tuskys");
        list.add("Nakumatt");

        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        s2.setAdapter(dataAdapter2);
    }
    if(sp1.equalsIgnoreCase("Kakamega")) {
        List<String> list = new ArrayList<String>();
        list.add("Yako");
        list.add("Nakumatt");
        list.add("Purchase");
        list.add("Mama Watoto");
        list.add("Tuskys Shilloah");
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        s2.setAdapter(dataAdapter2);
    }
    if(sp1.equalsIgnoreCase("Kisii")) {
        List<String> list = new ArrayList<String>();
        list.add("Oshwal");
        list.add("Naivas");
        list.add("Tuskys Chingware");
        list.add("Uchumi");
        list.add("Nakumatt");
        list.add("Chamunda");
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
          dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        s2.setAdapter(dataAdapter2);
    }

    if(sp1.equalsIgnoreCase("Kericho")) {
        List<String> list = new ArrayList<String>();
        list.add("Uchumi");
        list.add("Stagematt");
        list.add("Ukwala");
        list.add("Tuskys Ndonyo");
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        s2.setAdapter(dataAdapter2);
    }


Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);

// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        // creating new product in background thread
        new CreateNewProduct().execute();
    }
});
}

我希望它有所帮助!