我正在使用AlertDialog框,用户通过alertDialog框提供有关产品的说明。在对话框中有2个微调器,按钮等。当我选择旋转器选项时,它应该选择值并烘烤它..但它默认值。我需要在服务器上保存这个值...所以它应该是准确的,它保存服务器的默认值..我不知道哪里错了?作为一个新手我很困惑在这里...亲切的帮助。 这是我的SubMenu活动:
public class SubMenu extends AppCompatActivity {
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String RANK = "id";
static String COUNTRY = "name";
private ProgressDialog pDialog;
String status="";
static String FLAG = "image";
Integer i = 1;
private static String url_create_book = "http://cloud..com/broccoli/creatinfo.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_menu);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String SelectedId = getIntent().getStringExtra("id");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Get the view from listview_main.xml
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {
// @Override
// protected void onPreExecute() {
// super.onPreExecute();
// Create a progressdialog
// mProgressDialog = new ProgressDialog(SubMenu.this);
// Set progressdialog title
// mProgressDialog.setTitle("Categories of Main categories.....");
// Set progressdialog message
// mProgressDialog.setMessage("Loading...");
// mProgressDialog.setIndeterminate(false);
// Show progressdialog
// mProgressDialog.show();
// }
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonarray = JsonFunctions
.getJSONfromURL("http://cloud.granddubai.com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id"));
try {
// Locate the array name in JSON
// jsonarray = jsonobject.getJSONArray("main_menu_items");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
// map.put("id", jsonobject.getString("id"));
map.put("name", jsonobject.getString("name"));
map.put("image", jsonobject.getString("image"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.list1);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(SubMenu.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
listview.setOnItemClickListener(this);
// Close the progressdialog
// mProgressDialog.dismiss();
}
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long rowId) {
final AlertDialog customDialog = new AlertDialog.Builder(SubMenu.this).create();
customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
final LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.popup
, null);
customDialog.setView(dialogView);
final Spinner crust, adds;
// set the custom customDialogimation components - text, image and button
ImageLoader imageLoader= new ImageLoader(dialogView.getContext());
final ImageView desimage = (ImageView) dialogView.findViewById(R.id.desimage);
// final CircleImageView desimage =(CircleImageView)dialogView.findViewById(R.id.desimage);
imageLoader.DisplayImage(arraylist.get(position).get("image"), desimage);
final TextView heading = (TextView) dialogView.findViewById(R.id.h1);
final EditText spins = (EditText) dialogView.findViewById(R.id.spinst);
final String specialInstr=spins.getText().toString();
heading.setText(arraylist.get(position).get("name"));
final String hed=heading.getText().toString();
final Button dcr=(Button)dialogView.findViewById(R.id.decr);
final Button incre=(Button)dialogView.findViewById(R.id.incr);
final String crusttext, addstext;
/* crust = (Spinner)dialogView.findViewById(R.id.crst);
ArrayAdapter adapter = ArrayAdapter.createFromResource(SubMenu.this, R.array.Crust_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crust.setAdapter(adapter);
crust.setOnItemSelectedListener(this);
crusttext = crust.getSelectedItem().toString();
adds = (Spinner)dialogView.findViewById(R.id.crst);
ArrayAdapter adapteradds = ArrayAdapter.createFromResource(SubMenu.this, R.array.AddsOn_array, android.R.layout.simple_spinner_item);
adapteradds.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adds.setAdapter(adapteradds);
adds.setOnItemSelectedListener(this);
addstext = adds.getSelectedItem().toString();*/
final Button cncl = (Button) dialogView.findViewById(R.id.canc);
cncl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog.dismiss();
}
});
final ImageButton crssbtn = (ImageButton) dialogView.findViewById(R.id.btnCancel);
crssbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog.dismiss();
}
});
Button _decrease = (Button) dialogView.findViewById(R.id.incr);
Button _increase = (Button) dialogView.findViewById(R.id.decr);
final Spinner spinner1 = (Spinner) dialogView.findViewById(R.id.crst);
Spinner spinner2 = (Spinner) dialogView.findViewById(R.id.adson);
ArrayList<String> alspinner1 = new ArrayList<>();
ArrayList<String> alspinner2 = new ArrayList<>();
final String[] _spvalue1 = getResources().getStringArray(R.array.Crust_array);
final String[] _spvalue2 = getResources().getStringArray(R.array.AddsOn_array);
for (int i = 0; i < _spvalue1.length; i++) {
alspinner1.add(_spvalue1[i]);
}
for (int i = 0; i < _spvalue2.length; i++) {
alspinner2.add(_spvalue2[i]);
}
ArrayAdapter<String> adapter1 = new ArrayAdapter<>(dialogView.getContext(), android.R.layout.simple_spinner_item, alspinner1);
ArrayAdapter<String> adapter2 = new ArrayAdapter<>(dialogView.getContext(), android.R.layout.simple_spinner_item, alspinner2);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener((new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Object item = arg0.getItemAtPosition(arg2);
if (item!=null) {
Toast.makeText(SubMenu.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(SubMenu.this, "Selected",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}));
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(this);
addstext = spinner2.getSelectedItem().toString();
// Toast.makeText(SubMenu.this, addstext, Toast.LENGTH_SHORT).show();
final TextView _value = (TextView) dialogView.findViewById(R.id.value);
i = Integer.parseInt(_value.getText().toString());
final String _stringVal;
_decrease.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String _stringVal;
Log.d("src", "Decreasing value...");
if (i > 0) {
i = i - 1;
_stringVal = String.valueOf(i);
_value.setText(_stringVal);
Toast.makeText(SubMenu.this, ""+_stringVal, Toast.LENGTH_SHORT).show();
} else {
Log.d("src", "Value can't be less than 0");
}
}
});
_increase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String _stringVal;
Log.d("src", "Increasing value...");
i = i + 1;
_stringVal = String.valueOf(i);
_value.setText(_stringVal);
Toast.makeText(SubMenu.this, ""+_stringVal, Toast.LENGTH_SHORT).show();
}
});
Button altok=(Button)dialogView.findViewById(R.id.alrtok);
altok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//is chkIos checked?
pDialog = new ProgressDialog(SubMenu.this);
pDialog.setMessage("Please wait..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
final String spli=spins.getText().toString();
final RadioGroup rgroup = (RadioGroup) dialogView.findViewById(R.id.rgrp1);
final String rval1;
final RadioButton rbtn;
final int selectedId = rgroup.getCheckedRadioButtonId();
rbtn = (RadioButton) dialogView.findViewById(selectedId);
rval1 = rbtn.getText().toString();
Toast.makeText(SubMenu.this, rval1, Toast.LENGTH_SHORT).show();
Toast.makeText(SubMenu.this, spli, Toast.LENGTH_SHORT).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, url_create_book,
new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
pDialog.dismiss();
if (response.trim().equals("success")) {
// Toast.makeText(RegistrationForm.this,"Login Success",Toast.LENGTH_SHORT).show();
startActivity(new Intent(SubMenu.this, AddToCart.class));
//your intent code here
} else {
Toast.makeText(SubMenu.this,"Please Give Full Information!",Toast.LENGTH_SHORT).show();
}
}
},
new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.dismiss();
Toast.makeText(SubMenu.this, error.toString(), Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("dish_name", hed);
params.put("type",rval1);
// params.put("addson",sp);
// params.put("crust",crusttext);
params.put("special_instruction",specialInstr);
/*params.put("Last_Name",lname);
params.put("email", email);
params.put("Gender", rval);
params.put("password", passwrd);
params.put("confirmPasw",cpasswrd);
params.put("DOB",dobr);
params.put("sms_subscrb",status);
params.put("Mobile_No",mobile);*/
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(SubMenu.this);
requestQueue.add(stringRequest);
}
});
// crust.setOnItemSelectedListener(this);
// public void addListenerOnSpinnerItemSelection() {
/* final Button ok = (Button) dialogView.findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog.dismiss();
}
});*/
customDialog.show();
}
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
// Toast.makeText(SubMenu.this,"The planet is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
上面的字符串addstext和crusttext都是默认值..这是问题......
答案 0 :(得分:1)
class Demo extends Activity{
private Spinner sp1;
onCreate()
{
sp1=findviewbyid(id);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Object item = arg0.getItemAtPosition(arg2);
if (item!=null) {
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
SaveInDatabase(item.toString());
}
Toast.makeText(MainActivity.this, "Selected",
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
private void SaveInDatabase(String value)
{
//Logic to save in database;
}
}
答案 1 :(得分:0)
在onItemSelected Listener
中试试这个spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Object item = arg0.getItemAtPosition(arg2);
if (item!=null) {
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this, "Selected",
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});