我正在尝试将数据从回收站适配器传递给活动。目前,我正在尝试在下面使用更多意图的信息
适配器
public class BlogRecyclerAdapter extends RecyclerView.Adapter<BlogRecyclerAdapter.ViewHolder> {
public List<BlogPost> blog_list;
private FirebaseFirestore firebaseFirestore;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
private Context mContext;
public BlogRecyclerAdapter(Context context, List<BlogPost> blog_list) {
mContext = context;
this.blog_list = blog_list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item, parent, false);
firebaseFirestore = FirebaseFirestore.getInstance();
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
String desc_data = blog_list.get(position).getDesc();
holder.setDescText(desc_data);
String user_name = blog_list.get(position).getUser_id();
holder.setBlogUserName(user_name);
String image_url = blog_list.get(position).getImage_url();
holder.setBlogImage(image_url);
final String message_doc = blog_list.get(position).getMessage_doc();
/*
long millisecond = blog_list.get(position).getTimestamp().getTime();
String dateString = DateFormat.format("MM/dd/yyyy", new Date(millisecond)).toString();
holder.setTime(dateString);
*/
// String user_data = blog_list.get(position).getUser_id();
// holder.setUserData(user_data);
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = holder.getAdapterPosition();
//THIS IS WHAT NEEDS TO GET PASSED
String id = blog_list.get(pos).getPost_id();
/*
//IMPORTANT
Intent messageDocIntent = new Intent(mContext, BlogRecyclerAdapter.class);
messageDocIntent.putExtra("EXTRA_SESSION_ID", message_doc);
mContext.startActivity(messageDocIntent);
*/
Toast.makeText(mContext, message_doc, Toast.LENGTH_LONG).show();
// Toast.makeText(mContext, "test " + id, Toast.LENGTH_LONG).show();
Intent intent = new Intent(mContext, Confirmation_Activity.class);
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return blog_list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public Context mContext;
ConstraintLayout parentLayout;
private View mView;
private TextView descView;
private ImageView blogPostView;
private TextView blogDate;
private TextView blogUserName;
private CircleImageView blogUserImage;
private StorageReference mImageStorage;
private TextView userName;
@SuppressLint("ResourceType")
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
parentLayout = itemView.findViewById(R.id.Main_Blog_Post);
}
public void setDescText(String descText) {
descView = mView.findViewById(R.id.blog_desc);
descView.setText(descText);
}
public void setUserData(String downloadUrl, String name) {
mImageStorage = FirebaseStorage.getInstance().getReference();
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
final String current_uid = mCurrentUser.getUid();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mUserDatabase.keepSynced(true);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = Objects.requireNonNull(dataSnapshot.child("name").getValue()).toString();
StorageReference filepath = mImageStorage.child("profile_images").child(current_uid + (".jpeg"));
Log.d("heere", "S");
// This gets the download url async
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
//The download url
final String downloadUrl =
uri.toString();
Log.d("tag", downloadUrl);
if (!downloadUrl.equals("default")) {
// Glide.with(getApplicationContext()).load(downloadUrl).into(mDisplayImage);
Glide.with(itemView.getContext()).load(downloadUrl).into(blogUserImage);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
}
});
//Toast.makeText(getApplicationContext(), image, Toast.LENGTH_SHORT).show();
Log.d("value", dataSnapshot.getValue().toString());
Log.d("key", dataSnapshot.getKey());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void setTime(String date) {
blogDate = mView.findViewById(R.id.blog_date);
blogDate.setText(date);
}
public void setBlogImage(final String downloadUri) {
blogPostView = mView.findViewById(R.id.blog_image);
Glide.with(itemView.getContext()).load(downloadUri).into(blogPostView);
}
public void setBlogUserName(String userName) {
blogUserName = mView.findViewById(R.id.blog_user_name);
blogUserName.setText(userName);
}
}
}
如果您能为我指明正确的方向,那将不胜感激
当前问题
适配器
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
String desc_data = blog_list.get(position).getDesc();
holder.setDescText(desc_data);
String user_name = blog_list.get(position).getUser_id();
holder.setBlogUserName(user_name);
String image_url = blog_list.get(position).getImage_url();
holder.setBlogImage(image_url);
final String message_doc = blog_list.get(position).getMessage_doc();
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = holder.getAdapterPosition();
Toast.makeText(mContext, message_doc, Toast.LENGTH_LONG).show();
Intent intent = new Intent(mContext, Confirmation_Activity.class);
intent.putExtra("Message_ID", blog_list.get(pos).getMessage_doc());
mContext.startActivity(intent);
}
});
}
活动正在接收
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_activity);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mMessagedatabaseReference =
mFirebaseDatabase.getReference().child(messageID());
....
public String messageID(){
String id = getIntent().getStringExtra("Message_ID");
return id;
}
如果我在适配器活动中调试应用,则Message_Id等于firebase中的值,但是当我启动新活动时,应用崩溃并
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.debateapp/com.example.android.debateapp.ChatActivity}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
,错误指向mMessagedatabaseReference
答案 0 :(得分:0)
根据您的代码,您想将一个ID发送给Confirmation_Activity.class,然后您就可以像这样发送一个捆绑包:-
来自适配器:-
Intent intent = new Intent(mContext, Confirmation_Activity.class);
intent.putExtra("id", blog_list.get(position).getPost_id())
mContext.startActivity(intent);
在您的Confirmation_Activity.class中:-
String id = getIntent().getStringExtras("id");
答案 1 :(得分:0)
原来我正在尝试将Firestore ID实时传递