【问题标题】:same activity different listview相同的活动不同的列表视图
【发布时间】:2014-12-14 02:59:11
【问题描述】:

我一直在寻找解决方案,但找不到,所以我在这里。

现在我想要实现的是,当我单击一个菜单选项时,我想使用相同的菜单但使用不同的列表视图数据启动相同的活动。数据来自 SQLITE。

我所做的是,当我选择和选项(onoptionsitemselected)时,我传递了一些参数以了解从数据库中检索哪些数据,但它只适用于第一个选项,其他选项不起作用。

这是我认为我使用错误 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);
}

}

感谢和亲切的问候

【问题讨论】:

  • 嗨,您可以选择打开和关闭可见性。只需将两个列表视图都放在相同的位置,并且第二个 lv 的第一次可见性消失了。当您第二次需要另一个时,您可以看到第二个第一个消失, 简单和最好的主意谢谢
  • 使用 notifydatasetchanged() 方法而不是多次调用相同的活动。将适当的参数传递给您的适配器数据,并在 onOptionsItemSelected 方法上更新您的列表视图,而不是调用相同的活动
  • 完全按照@mcd 说的做,这就是你的解决方案
  • 大家好。在尝试 Göktay Kaykusuz 给出的答案之后,它的工作,但在阅读了你们之后,我已经尝试按照你说的去做。但没有运气。我已将 ListAdapter 更改为 BaseAdapter,并在通道 myList.Adapter(adapter) 之后添加了 adapter.notifydatasetchanged()。我还在每个 onoptionsitemsselected() 中添加了一个车道(不同的功能) platoList = controller.getAllEntremeses();并将此数据提供给适配器。我有些地方乱七八糟,但不知道亲切的问候

标签: android listview android-activity


【解决方案1】:

有些东西卡在我的脑海里,我认为这是你的问题的原因。 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);
}

【讨论】:

  • 感谢您的反馈,我很高兴能够提供帮助。这是我认为的罕见情况。我认为您的问题的根源是保存您的键值对的实例。由于您引用了相同的活动,因此实例不会被破坏。如果您在选项 1 之后选择选项 2,可能会发生下面的情况。对于每个 if 语句,第一个选项总是返回 true。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
  • 2016-02-01
  • 2023-03-04
  • 1970-01-01
相关资源
最近更新 更多