gpt4 book ai didi

android - 如何将数据从 Adapter 传递到 Fragment?

转载 作者:行者123 更新时间:2023-11-30 01:09:10 25 4
gpt4 key购买 nike

如何在adapterfragment之间传递数据?

我的适配器:

finalHolder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
finalHolder.spinner.setSelection(position);
selState = finalHolder.spinner.getSelectedItem().toString();
System.out.println(selState);
Toast.makeText(getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString("key", selState);
Log.i("BUNDLE", bundle.toString());
fragment.setArguments(bundle);
}
});

我的 fragment :

public  class MyListFragment extends Fragment{

ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);

/*Bundle bundle = this.getArguments();
String strtext = bundle.getString("key", " ");
System.out.println(strtext);*/
System.out.println("prima ancora");

Bundle arguments = this.getArguments();
System.out.println("prima");
if (arguments != null) {
System.out.println("dopo");

//String userId = arguments.getString("key");
//System.out.println("finalmente:"+userId);
user = getArguments().getString("Key");


}

}

最佳答案

您可以使用接口(interface)将数据从适配器传递到 fragment ,通常,您可以在这里找到它:Java - Interface

在您的情况下,您可以实现如下接口(interface):

Public Interface YourInterfaceName{
void onItemSelected(String key, String Value);
}

在你的 fragment 中你必须实现你的接口(interface):

public class YourFragment extends Fragment implements YourInterface
{
YourAdapter.setOnItemSelected(this);
}

并在您的适配器中执行此操作:

public class Adapter
{
public void setOnItemSelected(YourInterface yourIntrface)
{
this.yourIntrface = yourIntrface;
}
.......
finalHolder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
.....
//instead of creating instance of fragment do this:
yourInterface.onItemSelected(yourKey, value);
}

关于android - 如何将数据从 Adapter 传递到 Fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38591574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com