Android Studio 如何从 fragment 中获取 Intent ?

标签 android android-intent android-activity

这是我经过的地方:

public class AdapterForMainPage extends ArrayAdapter<String>{

        String friendID;

        AdapterForMainPage(Context context) {

                super(context, R.layout.mainpagerow);
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater friendsinflators = LayoutInflater.from(getContext());
                View custumView = friendsinflators.inflate(R.layout.mainpagerow, parent, false);

                final String friendID = getItem(position).toString();
                TextView friendsNameText = (TextView) custumView.findViewById(R.id.txtUsersNameMainPage);
                ImageButton lockImageButton = (ImageButton)custumView.findViewById(R.id.btnLock);

                friendsNameText.setText(friendID);
                lockImageButton.setImageResource(R.drawable.lockdefault);

                lockImageButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intentId = new Intent(getContext(), Message.class);
                            intentId.putExtra("friendID", friendID);
                            getContext().startActivity(intentId);

                            Intent intentMessageAdapter= new Intent(getContext(), AdapterForMessageList.class);
                            intentMessageAdapter.putExtra("friendID", friendID);
                        }
                });

                return custumView;
        }
}

这是我试图将它传递给的地方

public class AdapterForMessageList extends ArrayAdapter {

    public AdapterForMessageList(Context context) {
        super(context, R.layout.message_array);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater messageInflator = LayoutInflater.from(getContext());
        View custumView = messageInflator.inflate(R.layout.message_array, parent, false);

        DatabaseReference currentmessagefield;
        String currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid().toString();


        String messageKey = getItem(position).toString();


        currentmessagefield = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID).child("Friend").child(friendbID).child("Messages").child(messageKey);

        TextView txtMessage = (TextView)custumView.findViewById(R.id.txtMessage);
        txtMessage.setText(FriendUsername);

        return custumView;
    }
}

getIntent 不起作用。所以没有得到 Activity

最佳答案

Intent intentMessageAdapter= new Intent(getContext(), AdapterForMessageList.class);
                        intentMessageAdapter.putExtra("friendID", friendID);

您错过了开始 Activity 的线路

getContext().startActivity(intentMessageAdapter);

要在 fragment 中获取 Intent ,您必须使用 getAcivity().getIntent....;

关于Android Studio 如何从 fragment 中获取 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41647880/

相关文章:

android - 如何访问适用于 iOS 和 Android 的 Xamarin.Forms 中的目录路径?

android - 连接到互联网后如何自动刷新网页 View

android - 尝试将不透明图像附加到非不透明 ImageWriter 中,反之亦然

android - 使用复选框将 TextView 值发送到另一个 Activity 使用图像按钮 onclick

android - 如何通过没有收件人的 Intent 撰写短信

java - android fragment 内的 fragment

java - 这个 "1342177280"标志是干什么用的?

android - 使用 Intent 调用电话时需要权限吗?

java - Android 通知未启动 Activity

java - : Hand over points to GameOverActivity