在Android中如何在类外使用字符串值?
我正在使用AlertDialog box,其中用户通过AlertDialog box提供有关产品的说明。在对话框中有两个微调器、按钮等。当我选择微调器选项时,它应该采用选定的值并调整它,但它采用默认值 我需要在服务器上保存这个值,所以它应该是准确的;它正在将默认值保存到服务器。我不知道哪里不对 以下是我的子菜单活动:在Android中如何在类外使用字符串值?,android,spinner,Android,Spinner,我正在使用AlertDialog box,其中用户通过AlertDialog box提供有关产品的说明。在对话框中有两个微调器、按钮等。当我选择微调器选项时,它应该采用选定的值并调整它,但它采用默认值 我需要在服务器上保存这个值,所以它应该是准确的;它正在将默认值保存到服务器。我不知道哪里不对 以下是我的子菜单活动: public class SubMenu extends AppCompatActivity { JSONObject jsonobject; JSO
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(this);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(this);
final String spinner1Selection = alspinner1.get(spinner1.getSelectedItemPosition());
Toast.makeText(SubMenu.this, spinner1Selection, 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",spinner1Selection);
// 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);
}
}
}
公共类子菜单扩展AppCompative活动{
JSONObject JSONObject;
JSONArray JSONArray;
列表视图列表视图;
ListViewAdapter适配器;
进程对话框;
ArrayList ArrayList;
静态字符串RANK=“id”;
静态字符串COUNTRY=“name”;
私人对话;
字符串状态=”;
静态字符串FLAG=“image”;
整数i=1;
私有静态字符串url_create_book=”http://cloud..com/broccoli/creatinfo.php";
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_子菜单);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
字符串SelectedId=getIntent().getStringExtra(“id”);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//从listview_main.xml获取视图
//执行下载JSON异步任务
新建下载JSON().execute();
}
//下载JSON异步任务
私有类DownloadJSON扩展AsyncTask实现AdapterView.OnItemClickListener、AdapterView.OnItemSelectedListener{
//@覆盖
//受保护的void onPreExecute(){
//super.onPreExecute();
//创建一个progressdialog
//mProgressDialog=新建进度对话框(子菜单.this);
//设置进程对话框标题
//mProgressDialog.setTitle(“主要类别的类别…”);
//设置进程对话框消息
//设置消息(“加载…”);
//mProgressDialog.setUndeterminate(false);
//显示进度对话框
//mProgressDialog.show();
// }
@凌驾
受保护的Void doInBackground(Void…参数){
//创建一个数组
arraylist=新的arraylist();
//从给定的URL地址检索JSON对象
jsonarray=JsonFunctions
.getJSONfromURL(“http://cloud.granddubai.com/broccoli/menu_typeitem.php?id=“+getIntent().getStringExtra(“id”);
试一试{
//在JSON中找到数组名称
//jsonarray=jsonobject.getJSONArray(“主菜单项”);
for(int i=0;iArrayList<String> alspinner1 = new ArrayList<>();
ArrayList<String> alspinner2 = new ArrayList<>();
String spinner1Selection = alspinner1.get(spinner1.getSelectedItemPosition());
String spinner2Selection = alspinner2.get(spinner2.getSelectedItemPosition());
@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",
alspinner1.get(spinner1.getSelectedItemPosition()));
// 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;
}