Android : same activity different listview

on Saturday, December 13, 2014


I've been looking for a solution and can't find it so here I am.


Right now what I'm trying to achieve is when I click on an menu option I want to start the same activity with the same menu but with different listview data. The data is from SQLITE.


What I have done is when I select and option (onoptionsitemselected) I pass some arguments to know which data retrieve from database, but it just work the first option, the other ones dosen't work.


This is the code of my activity I think I'm using wrong the Intents



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);
}


}


Thanks and Kind Regards


0 comments:

Post a Comment