簡體   English   中英

誰能告訴我我做錯了什么,“將數據從一個片段發送到另一個片段”?

[英]Can anyone tell where i am doing it wrong, “sending the data from fragment to fragment”?

我正在片段之間發送數據,但我得到了空捆綁包。

我通過單擊按鈕調用在循環視圖中將數據從一個片段發送到另一個片段,該信息將通過捆綁包發送到呼叫日志。 按鈕設置在適配器上。 這是我的第一個問題,我可能會犯一個錯誤,如果有的話,我們深表歉意。

通訊接口:

public interface FragmentCommunication{

    void response(int position, String name, String designation);

}

這是片段1:

public class fragmentHome extends Fragment 

{

    private View view;
    private RecyclerView recyclerView;
    private List<contacts> contact = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.contactList);
        HomeAdaptor recycler = new HomeAdaptor(getContext(),contact,communication);
        recyclerView.setLayoutManager((new 
        LinearLayoutManager(getActivity())));
        recyclerView.setAdapter(recycler);
        return view;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contact.add(new contacts("Mehfooz","Android Developer"));
        contact.add(new contacts("Zabhi","IOS developer"));
        contact.add(new contacts("Ali","java coder"));
        contact.add(new contacts("Ahmed","testerr"));
        contact.add(new contacts("Aqib","Designer"));
        contact.add(new contacts("fahad","Project manager"));
        contact.add(new contacts("Mehfooz","Android Developer"));
        contact.add(new contacts("Zabhi","IOS developer"));
        contact.add(new contacts("Ali","java coder"));
        contact.add(new contacts("Ahmed","testerr"));
        contact.add(new contacts("Aqib","Designer"));
        contact.add(new contacts("fahad","Project manager"));
    }
    FragmentCommunication communication = new FragmentCommunication() {
        @Override
        public void response(int position, String name, String designation) {
            FragmentCall fragmentCall = new FragmentCall();
            Bundle bundle = new Bundle();
            bundle.putString("name" , name);
            bundle.putString("designation", designation);
            fragmentCall.setArguments(bundle);
        }
    };
}

這是“片段1適配器”視口偵聽器代碼:

    //making a phone call dialog is showing...
    makecall = new Dialog(context, android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen);
    makecall.setContentView(R.layout.make_a_call);
    viewHolder.callButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageButton endbutton = (ImageButton) makecall.findViewById(R.id.imageCallEndButton);
            TextView callName = (TextView) makecall.findViewById(R.id.makeCallTextName);
            TextView callDesignation =(TextView) makecall.findViewById(R.id.makeCallTextDesignation);
            callName.setText(data.get(viewHolder.getAdapterPosition()).getName());
            callDesignation.setText(data.get(viewHolder.getAdapterPosition()).getDesignation());
            Toast.makeText(context,"Calling...",Toast.LENGTH_LONG).show();
            makecall.show();
            //CallEnd button listener
            endbutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    makecall.dismiss();
                    Toast.makeText(context,"END CALL",Toast.LENGTH_SHORT).show();
                    communication.response(viewHolder.getAdapterPosition(),
                            data.get(viewHolder.getAdapterPosition()).getName(),
                            data.get(viewHolder.getAdapterPosition()).getDesignation());
                }
            });
            viewHolder.chatButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                }
            });
        }
    });
    return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.name.setText(data.get(position).getName());
    holder.designation.setText(data.get(position).getDesignation());
    Bundle bundle = new Bundle();
    bundle.putString("name", data.get(position).getName());
    bundle.putString("designation", data.get(position).getDesignation());
    fragmentCall.setArguments(bundle);
}

這是片段2:

public class FragmentCall extends Fragment{

    private View view;
    private List<callLog> callLog = new ArrayList<>();
    String name;
    String designation;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_fragment_call, container, false);
        RecyclerView recyclerView;
        recyclerView = (RecyclerView) view.findViewById(R.id.callList);
        CallAdaptor recycler = new CallAdaptor(getContext(),callLog);
        recyclerView.setLayoutManager((new LinearLayoutManager(getActivity())));
        recyclerView.setAdapter(recycler);
        return view;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        if(bundle != null){
            name = bundle.getString("name");
            designation = bundle.getString("designation");
            callLog.add(new callLog(name,designation));
            Toast.makeText(getContext(), "name" + name + "designation" + designation , Toast.LENGTH_LONG).show();
        }
        else
           Toast.makeText(getContext(), "bundle is null", Toast.LENGTH_LONG).show();
    }
}

除了從包中獲取的空數據,我沒有收到錯誤。 如果有人可以幫助我...謝謝!

您需要移動綁定視圖,然后將偵聽器單擊到onBindViewHolder而不是onCreateViewHolder 然后僅按位置獲取適配器項一次。

您不能在onBindViewHolder內創建捆綁,因為它將被多次調用並使您的應用程序被未使用的對象弄臟。 因此,您不應使用此:

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.name.setText(data.get(position).getName());
    holder.designation.setText(data.get(position).getDesignation());
    Bundle bundle = new Bundle();
    bundle.putString("name", data.get(position).getName());
    bundle.putString("designation", data.get(position).getDesignation());
    fragmentCall.setArguments(bundle);
}

使用這樣的東西:

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    // don't call this multiple times especially inside anonymous method
    // because the index position can be different from the previous call.
    int itemPosition = holder.getAdapterPosition();

    callLog datum = data.get(itemPosition);

    holder.name.setText(datum.getName());
    holder.designation.setText(datum.getDesignation());

    ...

    holder.yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ...
            communication.response(itemPosition, itemPosition.getName(),
                    itemPosition.getDesignation());
        }
    });

}
What you are missing here is these lines of code.Your are not adding new fragment on the same framelayout.

You have to add these lines as well after setting bundle in argument:
 FragmentManager fragmentManager = getFragmentManager();
 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
 fragmentTransaction.replace(R.id.container_view, fragmentCall);
 fragmentTransaction.commit();

//替換或添加您想要的任何內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM