有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在安卓中如何将数据从片段传递到下一页?

我想从fragment listview中获取数据,并在底部的表单布局中查看它。我想查看上面所附的图片。请帮帮我enter image description here

我完成了查看底部工作表的代码,当用户单击listview项目时,底部工作表将被打开。在底部工作表中,应该在底部工作表中查看片段中的listview项详细信息。我的项目代码附在下面

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_call_log, container, false);
    callLogList = (ListView) v.findViewById(R.id.callLogList);
    contactPhoto = (ImageView) v.findViewById(R.id.missedImage);

    final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(getActivity());
    View bottomSheetView = inflater.inflate(R.layout.bottom_sheet,null);
    bottomSheetDialog.setContentView(bottomSheetView);


   String[] textString = new String[]{"Play", "Share", "Call", "add Notes","Add To Block","Delete"};
  int[]  drawableIds = new int[]{R.drawable.play_icon, R.drawable.share_icon, R.drawable.call_icon, R.drawable.add_notes_icon,
            R.drawable.block_icon,R.drawable.delete_icon};

    final ListView listbottom = (ListView)bottomSheetDialog.findViewById(R.id.listBottomSheets);
    CustomAdapter adapter = new CustomAdapter(this,  textString, drawableIds);
    listbottom.setAdapter(adapter);




    listbottom.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {



            switch (position)
            {
                case 0:
                    Toast.makeText(getActivity(),"playing the song",Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(getActivity(),"sharing the song",Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(getActivity(),"deleting the song",Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(getActivity(),"blocking the song",Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(getActivity(),"nothing selected ",Toast.LENGTH_SHORT).show();
                    break;

            }

        }
    });


    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View) bottomSheetView.getParent());
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
    bottomSheetBehavior.setPeekHeight(320);

    bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            Toast.makeText(getActivity(),"Hidden",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {

        }
    });

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

    setRetainInstance(true);

    return v;
}

共 (2) 个答案

  1. # 1 楼答案

    通过序列化和反序列化联系人对象,可以将整个联系人对象传递到底部的工作表片段

    public class DetailFragment extends BottomSheetDialogFragment{ 
      private static final String DESCRIBABLE_KEY = "describable_key";
      private ContactModel contactToShow ;
    
      public static DetailFragment newInstance(ContactModel modelToPass) {
        DetailFragment bottomSheetFragment = new DetailFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(DESCRIBABLE_KEY, modelToPass);
        bottomSheetFragment .setArguments(bundle);
    
        return bottomSheetFragment ;
      } 
    
      @Override 
      public View onCreateView(LayoutInflater inflater,
          ViewGroup container, Bundle savedInstanceState) {
    
        //Deserilize contact object
        contactToShow = (ContactModel) getArguments().getSerializable(
            DESCRIBABLE_KEY);
    
        // The rest of your code to display detail of bill Gates
    
    } 
    

    之后,您可以开始执行如下操作:

      FragmentTransaction transaction = ((FragmentActivity) context)
                                .getSupportFragmentManager()
                                .beginTransaction();
    
    DetailFragment.newInstance(billGatesContactObject).show(transaction, "dialog_playback");
    

    你可以在这里看到工作示例

    https://github.com/dkim0419/SoundRecorder/blob/master/app/src/main/java/com/danielkim/soundrecorder/fragments/PlaybackFragment.java

    另一个肮脏的解决方案是将联系人对象保留在主机活动类中,并使用((HostActivity) getActivity).getContact()((HostActivity) getActivity).setContact(billGates)方法设置和获取联系人对象

  2. # 2 楼答案

    无论您想要从主listview传递到底部工作表列表的数据是什么,只需将这些数据传递到底部工作表适配器中即可。
    在这里排队

    CustomAdapter adapter = new CustomAdapter(this,  textString, drawableIds); 
    

    做出改变

    CustomAdapter adapter = new CustomAdapter(this, textString, drawableIds, myDataArrayListToDisplayInBottomSheet); 
    

    其中myDataArrayListToDisplayInBottomSheet 是必须在底页中显示的数据的ArrayList<>

    在你CustomAdapter中,使用这些数据来相应地显示