所以我对此做了一些研究,但无法解决这个问题,而且每个在线解决方案都会导致我出现新的错误。
我目前正在开发一个事件应用程序,而且对Android Studio来说相对较新。
我在管理员应用中有一个RecyclerView,用于验证组织者在Firebase上上传的所有事件数据。组织者上传event_tile,event_desc和event_image等数据。这是存储在Fire"事件"下的Firebase中。之后,管理应用程序以回收站视图的形式接收这些请求,并有一个批准它们的按钮。所有已批准的活动都将存储在Firebase中具有root" Approved_Events"的单独表格中。
我陷入了批准部分。代码运行正常,没有错误,但没有数据上传到我的Firebase控制台。
这是我的主要活动
public class MainActivity extends AppCompatActivity {
private RecyclerView request_eventList;
private DatabaseReference mRef, aRef;
private Button verify_button;
private TextView request_title, request_desc;
private ImageView request_image;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = new ProgressDialog(this);
mRef = FirebaseDatabase.getInstance().getReference().child("Event");
setContentView(R.layout.activity_main);
request_eventList = (RecyclerView) findViewById(R.id.request_eventList);
request_eventList.setHasFixedSize(true);
request_eventList.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Event, RequestViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, RequestViewHolder>(
Event.class,
R.layout.admin_event_row,
RequestViewHolder.class,
mRef
) {
@Override
protected void populateViewHolder(RequestViewHolder viewHolder, Event model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(), model.getImage());
}
};
request_eventList.setAdapter(firebaseRecyclerAdapter);
}
public static class RequestViewHolder extends RecyclerView.ViewHolder {
View mView;
public Button verify_button;
DatabaseReference mRef, aRef;
ProgressDialog progressDialog;
public RequestViewHolder(View itemView) {
super(itemView);
mView = itemView;
progressDialog = new ProgressDialog(mView.getContext());
verify_button = (Button) mView.findViewById(R.id.approve_button);
mRef = FirebaseDatabase.getInstance().getReference().child("Event");
aRef = FirebaseDatabase.getInstance().getReference().child("ApprovedEvents");
verify_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map <String, String> map = (Map<String, String>) dataSnapshot.getValue();
String title_val = map.get("title");
String desc_val = map.get("desc");
String image_val = map.get("image");
aRef.child("approved_title").setValue(title_val);
aRef.child("approved_desc").setValue(desc_val);
aRef.child("approved_image").setValue(image_val);
progressDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
public void setTitle(String title) {
TextView request_title = (TextView) mView.findViewById(R.id.request_title);
request_title.setText(title);
}
public void setDesc(String desc) {
TextView request_desc = (TextView) mView.findViewById(R.id.request_desc);
request_desc.setText(desc);
}
public void setImage(Context ctx, String image) {
ImageView request_image = (ImageView) mView.findViewById(R.id.request_image);
Picasso.with(ctx).load(image).into(request_image);
}
}
}
MainActivity XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.admin.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/request_eventList"
android:clickable="true">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
事件Java类
public class Event {
private String title, desc, image;
public Event(String title, String desc, String image) {
this.title = title;
this.desc = desc;
this.image = image;
}
public Event(){
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
admin_event_row将填充recyclerView的XML文件
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/request_image"
android:src="@drawable/abc_btn_check_material"
android:adjustViewBounds="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/request_title"
android:text="Title will come here "
android:padding="10dp"
android:textStyle="bold"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/request_desc"
android:text="Desc will come here "
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/approve_button"
android:text="Yeah it's Cool "/>
</LinearLayout>
答案 0 :(得分:1)
如果您的活动有更多未经批准的孩子,请将Verify_button.setOnClickListener从Holder移至Adapter-populateViewHolder。您必须将verify_button.setOnClickListener连接到recycleviewer中的行,以从单击的项目中获取title,desc和image的值。
protected void populateViewHolder(RequestViewHolder viewHolder, Event model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(), model.getImage());
final String title = model.getTitle();
final String desc = model.getDesc();
final String image = model.getImage();
final DatabaseReference aRef = FirebaseDatabase.getInstance().getReference();
viewHolder.verify_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String key = aRef.child("ApprovedEvents").push().getKey();
Event event = new Event(title, desc, image );
Map<String, Object> eventValues = event.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/ApprovedEvents/" + key, eventValues);
aRef.updateChildren(childUpdates);
}
});
}
};