将详细的火池数据发送到Android中的另一项活动
我是移动编程的新手,我想使用Firebase中的一些数据制作一个简单的Android应用程序。我设法与我的Firebase Collection的所有孩子一起创建了一个循环系统,我想按下按钮以导航到更详细的特定孩子(例如,查看更大的图像,完整的描述等)。在这里,我遇到了一些错误。我已经创建了一个适配器,这样:
public class MainAdapter extends FirebaseRecyclerAdapter<MainModel, MainAdapter.viewHolder> {
Context context;
public MainAdapter(@NonNull FirebaseRecyclerOptions<MainModel> options, Context context) {
super(options);
this.context = context;
}
@Override
protected void onBindViewHolder(@NonNull viewHolder holder, @SuppressLint("RecyclerView") final int position, @NonNull MainModel model) {
//viewHolder properties initialization
holder.btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("dresses");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String ma = snapshot.getValue().toString();
String name = snapshot.child("name").getValue().toString();
String brand = snapshot.child("brand").getValue().toString();
String description = snapshot.child("description").getValue().toString();
String image = snapshot.child("image").getValue().toString();
String url = snapshot.child("url").getValue().toString();
String price = snapshot.child("price").getValue().toString();
Intent intent = new Intent();
intent.putExtra("dress" , String.valueOf(ma));
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Intent intent = new Intent(context.getApplicationContext(), DetailActivity.class);
context.startActivity(intent);
}
});
}
}
因此,当我按下具有ID btnview的按钮时,我想导航到我的详细活动,这就是这样:
public class DetailActivity extends AppCompatActivity {
TextView name, brand, url, price, description;
ImageButton fav;
ImageView image;
ConstraintLayout view;
MainAdapter mainAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
name = findViewById(R.id.nameDress);
brand = findViewById(R.id.brandDress);
url = findViewById(R.id.urlDress);
url.setMovementMethod(LinkMovementMethod.getInstance());
price = findViewById(R.id.priceDress);
description = findViewById(R.id.descriptionDress);
fav = findViewById(R.id.imageButton);
image = findViewById(R.id.imageDress);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("dresses");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
name = (TextView) snapshot.child("name").getValue();
brand = (TextView) snapshot.child("brand").getValue();
description = (TextView) snapshot.child("description").getValue();
image = (ImageView) snapshot.child("image").getValue();
url = (TextView) snapshot.child("url").getValue();
price = (TextView) snapshot.child("price").getValue();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
@Override
protected void onStart() {
super.onStart();
mainAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
mainAdapter.stopListening();
}
}
基本上,我想找到一种从我的回收器视图项目发送数据的方法在按下视图按钮时,在我详细的活动的主要活动中。问题在于,上下文在我的适配器中似乎是无效的,因为我遇到了这个错误:
java.lang.nullpointerexception:尝试调用虚拟方法'android.content.context android.content.content.context.getapplicationcontext()'null对象
我试图在此组件中找到一种将其注入此组件的方法并非完全在Java中运作方式。我应该如何解决这个问题?
I am new to mobile programming and I want to make a simple android app using some data stored in Firebase. I managed to create a RecyclerView with all the children of my Firebase collection and I want to press a button to navigate to a more detailed view of a specific child (for example to see a bigger image, a complete description etc). Here I am encountering a few errors. I have created an adapter, like this:
public class MainAdapter extends FirebaseRecyclerAdapter<MainModel, MainAdapter.viewHolder> {
Context context;
public MainAdapter(@NonNull FirebaseRecyclerOptions<MainModel> options, Context context) {
super(options);
this.context = context;
}
@Override
protected void onBindViewHolder(@NonNull viewHolder holder, @SuppressLint("RecyclerView") final int position, @NonNull MainModel model) {
//viewHolder properties initialization
holder.btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("dresses");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String ma = snapshot.getValue().toString();
String name = snapshot.child("name").getValue().toString();
String brand = snapshot.child("brand").getValue().toString();
String description = snapshot.child("description").getValue().toString();
String image = snapshot.child("image").getValue().toString();
String url = snapshot.child("url").getValue().toString();
String price = snapshot.child("price").getValue().toString();
Intent intent = new Intent();
intent.putExtra("dress" , String.valueOf(ma));
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Intent intent = new Intent(context.getApplicationContext(), DetailActivity.class);
context.startActivity(intent);
}
});
}
}
So when I am pressing the button with the id btnView I want to navigate to my Detailed Activity, which is this:
public class DetailActivity extends AppCompatActivity {
TextView name, brand, url, price, description;
ImageButton fav;
ImageView image;
ConstraintLayout view;
MainAdapter mainAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
name = findViewById(R.id.nameDress);
brand = findViewById(R.id.brandDress);
url = findViewById(R.id.urlDress);
url.setMovementMethod(LinkMovementMethod.getInstance());
price = findViewById(R.id.priceDress);
description = findViewById(R.id.descriptionDress);
fav = findViewById(R.id.imageButton);
image = findViewById(R.id.imageDress);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("dresses");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
name = (TextView) snapshot.child("name").getValue();
brand = (TextView) snapshot.child("brand").getValue();
description = (TextView) snapshot.child("description").getValue();
image = (ImageView) snapshot.child("image").getValue();
url = (TextView) snapshot.child("url").getValue();
price = (TextView) snapshot.child("price").getValue();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
@Override
protected void onStart() {
super.onStart();
mainAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
mainAdapter.stopListening();
}
}
So basically I want to find a way to send the data from my recycler view item in the main activity to my detailed activity when pressing on the view button. The problem is that the context seems to be null in my adapter, because I'm getting this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
I tried to find a way to inject it in this component somehow, but I realised it's not exactly how it would normally work in Java. How should I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尚未在适配器代码中初始化上下文。可以通过将其传递到适配器构造函数中来初始化上下文
例如
例如
,要获得详细效果的主模型类,在ongreate()中,您可以:
You haven't initialized the context in the adapter code. The context could be initialized by passing it in the adapter constructor
e.g.
To pass the "MainModel" class to DetailsActivity, you can implement Parcelable or Serializable (I would go with Parcelable since it's faster) and send the "MainModel" class via intent
e.g.
And to get the MainModel class in DetailsActivity, in onCreate() you can: