gpt4 book ai didi

Android Studio 如何从 fragment 中获取 Intent ?

转载 作者:行者123 更新时间:2023-11-29 17:06:21 33 4
gpt4 key购买 nike

这是我经过的地方:

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/

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