android - onClickListener 方法中的捆绑包值问题
问题描述
我在我的 onClickListener 方法或任何其他一般方法中检索捆绑值时遇到了一些麻烦。除了代码之外,我还有一些评论以便更好地说明。所以会发生什么是我从activityA 中获取一个值,将它放在一个包中并将这些值传递给我的片段。我从包中获取值并在我的 setter 方法中设置,如果我敬酒它在屏幕上显示正确的值,我从包中获取的值不为空。下面我发布了我的整个 onCreateMethod,在过去的几天里我遇到了这个问题,所以非常感谢任何帮助。我认为发生的是,当我尝试在我的 onClickListener 方法中从捆绑包中获取值时,它出于某种原因将自身重置为 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;
}
已编辑
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();
我在我的片段中创建了一个 newInstance 方法。
public static TodoFragment newInstance(long id) {
TodoFragment fragment = new TodoFragment();
Bundle b = new Bundle();
b.putLong("fk",id);
fragment.setArguments(b);
return fragment;
}
解决方案
尝试以下操作:
- 由于您已经可以访问
fk_id
Fragment 中变量中的数据,因此可以将其标记为final
,并将其传递给 Toast:
Toast.makeText(getContext(), "dodano" + fk_id, Toast.LENGTH_LONG).show();
或者
- 尝试用synchronized和 your标记 your
getId_2
和,这样,两个方法之间共享的数据将被同步。setId_2
onCreateView()
推荐阅读
- datatables-1.10 - 空白行/带有 YADCF 数据表的数据
- javascript - GraphQL:包装外部api没有结果
- python - 在 Windows 平台上使用 Python 或 Node.js 将多页 .TIF 图像文件转换为 PDF 格式
- android - android将数组组合到循环中
- python - 为什么 Pandas read_excel 函数在 Pyinstaller .exe 中返回错误,但在 Python 解释器下没有?
- java - Java - java.io.NotSerializableException:net.sourceforge.jtds.jdbc.JtdsConnection
- angular - 在 Ionic 框架中使用 Chart.js 和 jQuery
- tensorflow - 即使队列运行器已经启动,Tensorflow session run() 也会挂起,为什么?
- c# - 如何在c#中设置给定格式的日期时间
- ios - 致命错误:在 UITableViewCell 中展开可选值错误时意外发现 nil?