我正在使用listview。我有一个活动类型menu.java,其中的项目来自listview中的服务器,例如披萨,意大利面,烤宽面条等。
如果我点击listview项目披萨或意大利面然后在下一个活动有另一个listview包含与比萨饼比萨饼或奶酪比萨饼等相关的项目等。如果我点击listView的cheese pizza或bianca pizza项目,将出现警告对话框,客户可以在其中进行选择。例如,对于意大利面,警报框将显示为披萨,但这次警报框必须不同。
我的问题是我无法为listView的每个项目显示不同的警告框(例如,item1的alertbox1或item2的alertbox2)
我也不能制作转换案例,因为它不是直接条件,我必须在我的SUBMENU活动和Typemenu活动的主要列表中进行更改。
这是我的SubMenu活动,其中将显示listview点击项目警告框。
我尝试过if if但它没有调用我的警告框:
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;
String _stringVal;
final int Pizza = 0; //should be equal to the index in your array.
final int Pasta = 1;
final int Lasagna = 2;
final int Salad =3 ;
final int Breakfast = 4;
final int Beverages= 5;
final int Soup = 6;
final int SpecialOffer =7;
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.....com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id"));
try {
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
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) {
if ( listview.getItemAtPosition(position).toString().equals("Bianca Pizza")||listview.getItemAtPosition(position).toString().equals("Cheese Pizza"))
{
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);
heading.setText(arraylist.get(position).get("name"));
final EditText spins = (EditText) dialogView.findViewById(R.id.spinst);
heading.setText(arraylist.get(position).get("name"));
final String hed = heading.getText().toString();
final Spinner spinner1 = (Spinner) dialogView.findViewById(R.id.crst);
final Spinner spinner2 = (Spinner) dialogView.findViewById(R.id.adson);
final ArrayList<String> alspinner1 = new ArrayList<>();
final 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]);
}
final ArrayAdapter<String> adapter1 = new ArrayAdapter<>(dialogView.getContext(), android.R.layout.simple_spinner_item, alspinner1);
final 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 spinner2Selection = alspinner2.get(spinner2.getSelectedItemPosition());
Toast.makeText(SubMenu.this, spinner2Selection, Toast.LENGTH_SHORT).show();
Button _decrease = (Button) dialogView.findViewById(R.id.incr);
Button _increase = (Button) dialogView.findViewById(R.id.decr);
final TextView _value = (TextView) dialogView.findViewById(value);
i = Integer.parseInt(_value.getText().toString());
_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) {
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) {
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));
} 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("crust", alspinner1.get(spinner1.getSelectedItemPosition()));
params.put("addson", alspinner2.get(spinner2.getSelectedItemPosition()));
params.put("quantity", _stringVal);
params.put("special_instruction", spli);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(SubMenu.this);
requestQueue.add(stringRequest);
}
});
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();
}
});
customDialog.show();
}
else if ( listview.getItemAtPosition(position).toString().equals("Pasta"))
{
final AlertDialog customDialog1 = new AlertDialog.Builder(SubMenu.this).create();
customDialog1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
final LayoutInflater inflater1 = getLayoutInflater();
final View dialogView1 = inflater1.inflate(R.layout.popup1, null);
customDialog1.setView(dialogView1);
final Button cncl1 = (Button) dialogView1.findViewById(R.id.canc1);
cncl1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog1.dismiss();
}
});
final ImageButton crssbtn1 = (ImageButton) dialogView1.findViewById(R.id.btnCancel1);
crssbtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog1.dismiss();
}
});
customDialog1.show();
}
else if ( listview.getItemAtPosition(position).toString().equals("Lasagna"))
{
final AlertDialog customDialog2 = new AlertDialog.Builder(SubMenu.this).create();
customDialog2.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
final LayoutInflater inflater2 = getLayoutInflater();
final View dialogView2 = inflater2.inflate(R.layout.popup2, null);
customDialog2.setView(dialogView2);
final Button cncl2 = (Button) dialogView2.findViewById(R.id.canc2);
cncl2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog2.dismiss();
}
});
final ImageButton crssbtn2 = (ImageButton) dialogView2.findViewById(R.id.btnCancel2);
crssbtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog2.dismiss();
}
});
customDialog2.show();
}
else if ( listview.getItemAtPosition(position).toString().equals("Salad"))
{
final AlertDialog customDialog3 = new AlertDialog.Builder(SubMenu.this).create();
customDialog3.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
final LayoutInflater inflater3 = getLayoutInflater();
final View dialogView3 = inflater3.inflate(R.layout.popup3, null);
customDialog3.setView(dialogView3);
final Button cncl3 = (Button) dialogView3.findViewById(R.id.canc3);
cncl3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog3.dismiss();
}
});
final ImageButton crssbtn3 = (ImageButton) dialogView3.findViewById(R.id.btnCancel3);
crssbtn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog3.dismiss();
}
});
customDialog3.show();
}
else if ( listview.getItemAtPosition(position).toString().equals("Breakfast"))
{
final AlertDialog customDialog4 = new AlertDialog.Builder(SubMenu.this).create();
customDialog4.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
final LayoutInflater inflater4 = getLayoutInflater();
final View dialogView4 = inflater4.inflate(R.layout.popup4, null);
customDialog4.setView(dialogView4);
final Button cncl4 = (Button) dialogView4.findViewById(R.id.canc4);
cncl4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog4.dismiss();
}
});
final ImageButton crssbtn4 = (ImageButton) dialogView4.findViewById(R.id.btnCancel3);
crssbtn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog4.dismiss();
}
});
customDialog4.show();
}
else if ( listview.getItemAtPosition(position).toString().equals("Beverages"))
{
final AlertDialog customDialog5 = new AlertDialog.Builder(SubMenu.this).create();
customDialog5.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
final LayoutInflater inflater5 = getLayoutInflater();
final View dialogView5 = inflater5.inflate(R.layout.popup5, null);
customDialog5.setView(dialogView5);
final Button cncl5 = (Button) dialogView5.findViewById(R.id.canc5);
cncl5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog5.dismiss();
}
});
final ImageButton crssbtn5 = (ImageButton) dialogView5.findViewById(R.id.btnCancel5);
crssbtn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog5.dismiss();
}
});
customDialog5.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);
}
}
}
我有一个不同的2布局不同的警报框。这里我只是调整第一个listview条件...
logcat:
FATAL EXCEPTION: main
Process: com.example.zeba.broccoli, PID: 28504
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.zeba.broccoli.SubMenu$DownloadJSON.onItemClick(SubMenu.java:161)
at android.widget.AdapterView.performItemClick(AdapterView.java:339)
at android.widget.AbsListView.performItemClick(AbsListView.java:1532)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3700)
at android.widget.AbsListView$3.run(AbsListView.java:5684)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199