这是我经过的地方:
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/