从片段的片段中打开片段?

时间:2019-02-25 20:16:49

标签: android android-fragments

我有一个带有ListView的片段。当我单击一个项目时,将打开一个新的片段,然后从该片段中打开一个带有按钮的新片段。 我尝试编写代码,但是很混乱。当单击按钮时,程序崩溃。 我应该使用其他方式吗?我将第二个片段称为第二个

活动:

 public class Grade extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceSt

ate);
        setContentView(R.layout.activity_grade);

        BottomNavigationView bottomNav = findViewById(R.id.lista_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_list,
                    new Objectlist()).commit();
        }
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;

                    switch (item.getItemId()) {
                        case R.id.nav_tantargylista:
                            selectedFragment = new Objectlist();
                            break;
                        case R.id.nav_dolgozatlista:
                            selectedFragment = new Examlist();
                            break;
                    }

                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_list,
                            selectedFragment).commit();

                    return true;
                }
            };
}

第一个片段:

public class Objectlist  extends Fragment {

    View v;
    DB mydb;
    ListView listView;
    private String teszt;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_objectlist, container, false);

        listView = (ListView)v.findViewById(R.id.Gradeview);
        mydb = new DB(getActivity());

        final ArrayList<String> thelist = new ArrayList<>();
        Cursor data = mydb.getTantargynev();

        if (data.getCount() == 0) {
            Toast.makeText(getActivity(), "Nincs jegyek hozzáadva", Toast.LENGTH_SHORT).show();
        }
        else {
            while (data.moveToNext()) {
                thelist.add(data.getString(0));
                ListAdapter listadapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, thelist);
                listView.setAdapter(listadapter);
            }


            listView.setOnItemClickListener(
                    new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                            teszt = thelist.get(i);
                            Bundle bundle = new Bundle();

                            String jegyAtlag="0";
                            Cursor data = mydb.JegyekAtlaga(teszt);
                            while (data.moveToNext()) jegyAtlag=data.getString(0);

                            String jegyDarab="0";
                            data = mydb.JegyekDarabszama(teszt);
                            while (data.moveToNext()) jegyDarab=data.getString(0);

                            if (jegyAtlag.equals("") || jegyDarab.equals(""))

                            else {
                                bundle.putString("Tantárgy átlaga", jegyAtlag);
                                bundle.putString("Tantárgy darabszáma", jegyDarab);

                                TextView jegyekHeader = (TextView) v.findViewById(R.id.header);
                                jegyekHeader.setText(teszt);

                                Fragment targyAdatok = new targyAdatok();

                                Fragment jegyekAllando = new jegyekAllando();
                                jegyekAllando.setArguments(bundle);
                                FragmentTransaction FragTan = getActivity().getSupportFragmentManager().beginTransaction();
                                FragTan.replace(R.id.jegyekMenu, targyAdatok);
                                ListView listaNezet = (ListView) v.findViewById(R.id.Gradeview);
                                listaNezet.setVisibility(View.GONE);
                                FragTan.commit();
                            }
                        }
                    }
            );
        }

        return v;
    }
}

第二个片段:

公共类targyAdatok扩展了片段{

public targyAdatok() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_targy_adatok, container, false);

    Button elemzes = (Button)v.findViewById(R.id.elemzes);
    elemzes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Fragment jegyekAllando = new jegyekAllando();
            FragmentTransaction FragTan = getActivity().getSupportFragmentManager().beginTransaction();
            FragTan.replace(R.id.targyAdatok,jegyekAllando);
            FragTan.commit();
        }
    });

    return v;
}

}

第三个片段:

    public class jegyekAllando extends Fragment {

        DB mydb;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_jegyek_allando, container, false);

Bundle bundle = getArguments();

        String jegyAtlagSt = bundle.getString("Tantárgy átlaga");
        String jegyDarabSt = bundle.getString("Tantárgy darabszáma");

            return rootView;
        }
    }

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。问题是a替换了错误的片段。每次我必须替换活动的片段。