onClickListener方法

时间:2018-06-13 14:20:58

标签: android android-fragments

我在onClickListener方法或任何其他方法中检索包值时遇到了一些麻烦。除了代码之外,我还有其他意见以便更好地澄清。所以会发生什么,我从activityA获取一个值,将它放在一个包中,并将值传递给我的片段。我从包中获取值并在我的setter方法中设置我从包得到的值不为空如果我在屏幕上显示正确值的值。下面我发布我的整个onCreateMethod,我在过去几天有这个问题所以任何帮助非常感谢。我认为发生的是当我尝试从我的onClickListener方法中获取bundle中的值时,由于某种原因它将自身重置为0。

感谢您的提前帮助

片段中的onCreateView方法

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         final Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_todo_list, container, false);
    FloatingActionButton floatingActionButton = view.findViewById(R.id.fab);
    recyclerView = (RecyclerView) view.findViewById(R.id.list);

    bundle = this.getArguments();
    if(bundle != null) {
        fk_id = bundle.getLong("fk");
        setId_2(fk_id); // i try to set the value
        Toast.makeText(getContext(), "iz fragmenta" + getId_2(), Toast.LENGTH_SHORT).show(); // this prints the correct value
        floatingActionButton.setOnClickListener(new View.OnClickListener() { //this button stops working if in the if statement
            @Override
            public void onClick(View v) {
                LayoutInflater li = LayoutInflater.from(getActivity());
                View popupView = li.inflate(R.layout.popup_layout, null);
                final EditText editText = popupView.findViewById(R.id.userInput);
                AlertDialog.Builder adb = new AlertDialog.Builder(getContext());

                adb.setView(popupView);
                adb.setCancelable(false)
                        .setPositiveButton("Dodaj", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String naziv = editText.getText().toString();
                                Aktivnost_ ak = new Aktivnost_(naziv, "15-jun", fk_id, "kajetan", "todo");
                                dodajAktivnost(ak);
                                array.add(ak);
                                Toast.makeText(getContext(), "dodano" + getId_2(), Toast.LENGTH_LONG).show();

                            }
                        })
                        .setNegativeButton("Prekliči", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.cancel();
                                Toast.makeText(getContext(), "Preklical sem", Toast.LENGTH_LONG).show();
                            }
                        });
                AlertDialog alertDialog = adb.create();
                alertDialog.setCancelable(true);
                alertDialog.show();
            }
        });


        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
        mmAdapter = new ToDoRecyclerViewAdapter(listAktivnosti(getId_2()), getContext(), mListener);

        mmAdapter.setOnItemClickListner(new ToDoRecyclerViewAdapter.onItemClickListner() {
            @Override
            public void onClick(long i) {
                Intent intent = new Intent(getActivity(), PodrobnostiActivity.class);
                intent.putExtra("key_id", i);
                startActivity(intent);
                Toast.makeText(getContext(), "" + i, Toast.LENGTH_SHORT).show();
            }
        });
        mmAdapter.setOnLongClick(new ToDoRecyclerViewAdapter.OnLongClickListener_() {
            @Override
            public void onLongClick(long i, String item) {
                if (item.equals("doing")) {
                    boolean update_1 = db.updateList(i, item);
                    if (update_1) {
                        //NAREDI SE LEPE ANIMACIJE
                        android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
                        ft.detach(TodoFragment.this).attach(TodoFragment.this).commit();

                        Toast.makeText(getContext(), "Dodano v bazo.!", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getContext(), "Prislo je do napake!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        recyclerView.setAdapter(mmAdapter);
    }  
    return view;
}

EDITED

    private long fk_id;
public long getId_2() {
    return fk_id;
}
public void setId_2(long fk_id) {
    this.fk_id = fk_id;
}

EDIT_2

这部分是我的活动onCreate

    Intent intent = getIntent();
    long id = intent.getLongExtra("intVariableName",0);

android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container, TodoFragment.newInstance(id), "a");
    fragmentTransaction.commit();

我在我的片段中创建了一个新的实例方法。

    public static TodoFragment newInstance(long id) {
    TodoFragment fragment = new TodoFragment();
    Bundle b = new Bundle();
    b.putLong("fk",id);
    fragment.setArguments(b);
    return fragment;
}

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

  1. 由于您已经可以访问片段中变量fk_id中的数据,因此可以将其标记为final,并将其传递给您的Toast:
  2. Toast.makeText(getContext(), "dodano" + fk_id, Toast.LENGTH_LONG).show();

    OR

    1. 尝试使用同步以及getId_2标记setId_2onCreateView(),这样,两种方法之间共享的数据就会同步。