【问题标题】:Put Sqlite data into two ListView inside two different Fragments将Sqlite数据放入两个不同Fragment内的两个ListView
【发布时间】:2018-05-27 19:05:22
【问题描述】:

基本上,我想根据 Fragment 的可见性从两个不同的表(或列)中从 Sqlite 获取数据。我已经找到了一个解决方案(棘手的解决方案),我想知道是否有其他最好的方法可以达到相同的结果。我只在一个片段上建立了很多教程。下面是我所做的:

首先,在我的 MainActivity 我有:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    toolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(),"Hai cliccato la barra",Toast.LENGTH_SHORT).show();
            fragment = new HomeFragment();
            if (fragment!=null) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction ft = fragmentManager.beginTransaction();

                ft.replace(R.id.screen_area,fragment);
                ft.commit();

            }
        }
    });

我可以从 navigationView 中选择我想看的片段:

public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {

    }
    if (fragment!=null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        ft.replace(R.id.screen_area,fragment);
        ft.commit();
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    int id= item.getItemId();

    switch(id){

        case R.id.carburante:
            fragment = new CarburanteFragment();
            break;
        case R.id.giorni:
            fragment = new GiorniFragment();
            break;

...在我的 CarburanteFragment 之后,我将片段传递给 DbAdapter...例如在“保存”中:

private void save(String name)
    {
        DBAdapter db=new DBAdapter(getActivity(),((MainActivity) getActivity()).fragment= this);
        db.openDB();
        boolean saved=db.add(name);
        if(saved)
        {
            nameEditText.setText("");
            this.getSpacecrafts();
        }else {
            Toast.makeText(getContext(),"Unable To Save",Toast.LENGTH_SHORT).show();
        }
    }

...最后在 DBAdapter 类中,我可以得到我想要填充 ListView 的片段....例如更改常量变量等等....在下面的代码中,我只测试了它是否有效:

 public DBAdapter(Context c,Fragment fr) {
    this.c = c;
    helper = new DBHelper(c);


  Fragment currentFragment =fr;
           if(currentFragment.getTag()=="carburante")
             Toast.makeText(c, currentFragment.toString(), Toast.LENGTH_LONG).show();


}

【问题讨论】:

  • 我不知道你的情况是否有意义。但是,您可以在托管两个片段的活动中存储一个标志,然后将此标志用于查询数据库吗?
  • 或者,如果您在片段中显示数据库数据,那么您可以相应地查询数据库。就像你有两个片段 HumansFragAnimalFrag。然后查询HumansTable里面HumansFragAnimalTable里面AnimalFrag
  • 是的,我在片段内的 ListView 中显示数据......但我有两个不同的片段和两个不同的 listView......问题是如何从 DB 中选择要加载的数据...... .我的解决方案在上面。 Arnetatively 我认为可以只加载一个片段和一个 ListView 并在其中加载正确的数据,一旦我知道(可能从按钮单击)我想要哪种类型......但此时我不'不知道如何实现它:-)
  • 当你替换片段时,你可以在 bundle 中传递一些数据并将其设置为参数,如 MyFrag frag = new MyFrag(); Bundle bundle = new Bundle() bundle.put("FLAG", Constant.DATA1) frag.setArgument(bundle) 然后你可以在你的和使用中检索这个参数这以确定您必须加载哪些数据。
  • 我按照您的提示...非常有用...谢谢!

标签: android sqlite listview android-fragments


【解决方案1】:

你可以做的是你可以在替换片段时传递一个标志,以后可以使用它来识别你想要加载的数据。

对于片段1

Fragment1 frag = new Fragment1();
Bundle bundle = new Bundle ();
bundle.put("MY_FLAG", 1);
frag.setArgument(bundle);

对于片段2

Fragment2 frag = new Fragment2();
Bundle bundle = new Bundle ();
bundle.put("MY_FLAG", 2);
frag.setArgument(bundle);

然后你可以使用flag中的数据来识别你要加载的数据类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多