我一直在寻找一个解决方案,但我找不到它。
现在我想要实现的是当我点击一个菜单选项时,我希望使用相同的菜单但使用不同的列表视图数据启动相同的活动。数据来自SQLITE。
我所做的是当我选择和选项(onoptionsitemselected)时,我传递一些参数来知道从数据库中检索哪些数据,但它只是工作第一个选项,其他选项不起作用。
这是我的活动代码,我认为我使用了错误的意图
public class PlantillaChina extends ActionBarActivity{
// DB Class to perform DB related operations
DBController controller = new DBController(this);
// Progress Dialog Object
ProgressDialog prgDialog;
HashMap<String, String> queryValues;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.lista);
Intent myIntent = getIntent();
Intent myIntent2 = getIntent();
Intent myIntent3 = getIntent();
Intent myIntent4 = getIntent();
String value = myIntent.getStringExtra("entremeses");
String value2 = myIntent2.getStringExtra("arroces");
String value3 = myIntent3.getStringExtra("mar");
String value4 = myIntent4.getStringExtra("carnes");
// Get plato records from SQLite DB
ArrayList<HashMap<String, String>> platoList = new ArrayList<HashMap<String, String>>();
if (value.equals("entremeses")){
platoList = controller.getAllEntremeses();
}else if(value2.equals("arroces")){
platoList = controller.getAllArrocesyPasta();
}else if(value3.equals("mar")){
platoList = controller.getAllMar();
}else if(value4.equals("carnes")){
platoList = controller.getAllCarnes();
}
// If plato exists in SQLite DB
if (platoList.size() != 0) {
// Set the plato Array list in ListView
ListAdapter adapter = new SimpleAdapter(PlantillaChina.this, platoList, R.layout.itemlista, new String[] {
"platoNombre", "platoDescripcion", "platoPrecio" }, new int[] { R.id.nombre, R.id.descripcion, R.id.precio });
ListView myList = (ListView) findViewById(R.id.listaplatos);
myList.setAdapter(adapter);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.china, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
// if (id == R.id.action_settings) {
// return true;
switch (item.getItemId()){
case R.id.entremeses:
Intent myIntent=new Intent(this,PlantillaChina.class);
myIntent.putExtra("entremeses", "entremeses");
this.startActivity(myIntent);
break;
case R.id.arrocesypasta:
Intent myIntent2=new Intent(this,PlantillaChina.class);
myIntent2.putExtra("arroces", "arroces");
this.startActivity(myIntent2);
break;
case R.id.pescados:
Intent myIntent3=new Intent(this,PlantillaChina.class);
myIntent3.putExtra("mar", "mar");
this.startActivity(myIntent3);
break;
case R.id.carnes:
Intent myIntent4=new Intent(this,PlantillaChina.class);
myIntent4.putExtra("carnes", "carnes");
this.startActivity(myIntent4);
break;
}
return super.onOptionsItemSelected(item);
}
}
谢谢和亲切的问候
答案 0 :(得分:0)
有些东西卡在我脑海里,我认为这是你问题的原因。 getStringExtra()
和putExtra()
方法使用键值对,而if-else语句具有奇怪的结构。为什么我们不尝试下面的?
public class PlantillaChina extends ActionBarActivity{
// DB Class to perform DB related operations
DBController controller = new DBController(this);
// Progress Dialog Object
ProgressDialog prgDialog;
HashMap<String, String> queryValues;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.lista);
Intent myIntent = getIntent();
String value = myIntent.getStringExtra("key");
// Get plato records from SQLite DB
ArrayList<HashMap<String, String>> platoList = new ArrayList<HashMap<String, String>>();
if (value.equals("entremeses")){
platoList = controller.getAllEntremeses();
}else if(value.equals("arroces")){
platoList = controller.getAllArrocesyPasta();
}else if(value.equals("mar")){
platoList = controller.getAllMar();
}else if(value.equals("carnes")){
platoList = controller.getAllCarnes();
}
// If plato exists in SQLite DB
if (platoList.size() != 0) {
// Set the plato Array list in ListView
ListAdapter adapter = new SimpleAdapter(PlantillaChina.this, platoList, R.layout.itemlista, new String[] {
"platoNombre", "platoDescripcion", "platoPrecio" }, new int[] { R.id.nombre, R.id.descripcion, R.id.precio });
ListView myList = (ListView) findViewById(R.id.listaplatos);
myList.setAdapter(adapter);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.china, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
// if (id == R.id.action_settings) {
// return true;
switch (item.getItemId()){
case R.id.entremeses:
Intent myIntent=new Intent(this,PlantillaChina.class);
myIntent.putExtra("key", "entremeses");
this.startActivity(myIntent);
break;
case R.id.arrocesypasta:
Intent myIntent2=new Intent(this,PlantillaChina.class);
myIntent2.putExtra("key", "arroces");
this.startActivity(myIntent2);
break;
case R.id.pescados:
Intent myIntent3=new Intent(this,PlantillaChina.class);
myIntent3.putExtra("key", "mar");
this.startActivity(myIntent3);
break;
case R.id.carnes:
Intent myIntent4=new Intent(this,PlantillaChina.class);
myIntent4.putExtra("key", "carnes");
this.startActivity(myIntent4);
break;
}
return super.onOptionsItemSelected(item);
}