我有一个活动我开始使用片段来选择一些列表,在选择之后我想要获得片段中所选内容的列表。
我用了onActivityResult()
...... !!但它没有用!
请帮助!
这是我的活动代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RecyclerView foldersRV;
private RecyclerView.Adapter foldersAdapter;
private AppCompatButton addFolderBtn;
private AppCompatTextView hiddenText;
private List<String> selectedFoldersToHide = new ArrayList<>();
private static final int REQ_FOR_FOLDERS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
addFolderBtn = (AppCompatButton) findViewById(R.id.add_folder_btn);
hiddenText = (AppCompatTextView) findViewById(R.id.hidden_text_view);
foldersRV = (RecyclerView) findViewById(R.id.folders_list_recycler_view);
foldersRV.setVisibility(View.GONE);
hiddenText.setVisibility(View.VISIBLE);
addFolderBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_folder_btn:
moveToFragment(new FoldersHiddenFragment(), "Add Folders Fragment");
}
}
private void moveToFragment(Fragment toFragment, String TAG) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG);
if (fragment != null && fragment.isVisible()) {
System.out.println("*** ALREADY there ****");
} else {
transaction.replace(R.id.main, toFragment, TAG);
transaction.addToBackStack(null).commit();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQ_FOR_FOLDERS) {
selectedFoldersToHide = data.getStringArrayListExtra("FOLDERS_TO_HIDE");
}
}
}
以下是我的片段代码:
public class FoldersHiddenFragment extends Fragment {
private View allFoldersView;
private RecyclerView foldersRV;
private RecyclerView.Adapter foldersAdapter;
private AppCompatButton hideFoldersBtn;
private ArrayList<String> selectedFoldersToHide = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
allFoldersView = inflater.inflate(R.layout.folders_hidden_fragment_view, container, false);
initView();
return allFoldersView;
}
private void initView() {
foldersRV = (RecyclerView) allFoldersView.findViewById(R.id.folders_list_recycler_view);
foldersRV.setHasFixedSize(true);
foldersRV.setLayoutManager(new LinearLayoutManager(getActivity()));
MyFiles myFiles = new MyFiles();
foldersAdapter = new FoldersAdapter(getActivity(), myFiles.getAllDirectoriesNames(), new FoldersAdapter.FileInterface() {
@Override
public void getFilesSelected(List<String> folderSelectedList) {
selectedFoldersToHide = (ArrayList<String>) folderSelectedList;
}
});
foldersRV.setAdapter(foldersAdapter);
foldersAdapter.notifyDataSetChanged();
hideFoldersBtn = (AppCompatButton) allFoldersView.findViewById(R.id.hide_folder_btn);
hideFoldersBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("******** TOTAL SELECTED: " + selectedFoldersToHide.size());
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putStringArrayListExtra("FOLDERS_TO_HIDE", selectedFoldersToHide);
getActivity().setResult(RESULT_OK, intent);
getActivity().getSupportFragmentManager().popBackStack();
}
});
}
}
答案 0 :(得分:2)
创建一个名为PassArrayList
的界面,并在Activity
。
public interface passArrayList {
public void onItemsSelected(List<String> selectedItems);
}
public class MainActivity extends AppCompatActivity implements passArrayList{
// ...
@override
public void onItemsSelected(List<String> selectedItems){
//your code goes here
}
}
在您的片段中,添加以下内容:
PassArrayList mCallback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallback = (PassArrayList) getActivity();
}
@Override
public void onDetach() {
super.onDetach();
mCallback = null;
}
在您的代码中的某个位置,如果您想将歌曲列表传回Activity
,请调用onItemsSelected()
对象上的方法mCallback
并将ArrayList
传递给此方法ArrayList
1}}。然后,此onItemsSelected()
将作为Activity
中方法mCallback
的参数,您可以随心所欲地使用它。但请不要忘记在onDetach()
挂钩方法中取消curl
的链接,以防止上下文泄漏。
答案 1 :(得分:0)
1)这是将数据从片段传递到活动的基本示例:
在片段A中
((ParentActivity)getActivity()).dispatchInformations(list);
在ParentActivity中
public void dispatchInformations(List dataList){
}
2)或者在活动传递引用片段中创建一个接口并实现,并将结果放入活动中。
答案 2 :(得分:0)
最简单的方法是使用EventBus:
public static class MessageEvent { /* your arraylist here */ }
//Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode in Activity:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
// send event from your Fragment
EventBus.getDefault().post(new MessageEvent(YourArrayList));