gpt4 book ai didi

Android - 将 Intent 从一个 Activity 传递到另一个使用 Firebase 实现回调类的 Activity

转载 作者:行者123 更新时间:2023-11-30 01:08:31 28 4
gpt4 key购买 nike

我有两个 Activity :AddUserToDoToDo 实现了一个带回调的类。 ToDo 允许用户创建待办事项列表,待办事项将立即显示在 recyclerView 中。用户可以在 ToDo 中添加、更新或删除待办事项。

添加用户.java

public class AddUser extends AppCompatActivity implements View.OnClickListener{

private DatabaseReference mUserRef;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_user);

mUserRef = FirebaseDatabase.getInstance().getReference().child("users");

EditText etUserid = (EditText) findViewById(R.id.etUserid);
EditText etUsername = (EditText) findViewById(R.id.etUsername);
Button btnNext = (Button) findViewById(R.id.btnNext);

btnNext.setOnClickListener(this);
}

public void addUser(UserDetails userDetails){
userPushKey = mUserRef.push().getKey();
mUserRef.child(userPushKey).setValue(userDetails);
}

@Override
public void onClick(View v){
if(v == btnNext){
String inputUserid = etUserid.getText().toString();
String inputUsername = etUsername.getText().toString();

addUser(new UserDetails(inputUserid, inputUsername));

Intent intent = new Intent(AddUser.this,ToDo.class);
intent.putExtra("userKeyRef", userPushKey);
startActivity(intent);
}
}
}

ToDo.java

public class ToDo extends AppCompatActivity implements UserTodoAdapter.Callback {

private UserTodoAdapter mAdapter;

@Override
protcted void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);

mAdapter = new UserTodoAdapter(this);
RecyclerView view = (RecyclerView) findViewById(R.id.recycler_view);
view.setHasFixedSize(true);
view.setAdapter(mAdapter);
}

@Override
public void onEdit(final UserTodo userTodo){
// some functions here
}
}

UserTodoAdapter.java

public class UserTodoAdapter extends RecyclerView.Adapter<UserTodoAdapter.ViewHolder> {
private List<UserTodo> mUserTodo;
private Callback mCallback;
private DatabaseReference mUserTodoRef;

public UserTodoAdapter(Callback callback) {
mCallback = callback;
mUserTodo = new ArrayList<>();

// need to get the push key from AddUser activity
mUserTodoRef = FirebaseDatabase.getInstance.getReference().child(users).child("Need the push key here").child("todo");
mUserTodoRef.addChildEventListener(new TodoChildEventListener());
}

private class TodoChildEventListener implements ChildEventListener{
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s){
// action here
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s){
// action here
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot){
// action here
}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s){
// action here
}

@Override
public void onCancelled(DatabaseError databaseError){
// action here
}
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_custom_view, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position){
final UserTodo userTodo = mUserTodo.get(position);
holder.mTodoTitle.setText(userTodo.getTodoTitle());
holder.mTodoDesc.setText(userTodo.gerTodoDesc());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallback.onEdit(userTodo);
}
});

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
removeTodo(mUserTodo.get(position));
return true;
}
});
}

@Override
public int getItemCount(){
return mUserTodo.size();
}

public interface Callback{
public void onEdit(UserTodo userTodo);
}

class ViewHolder extends RecyclerView.ViewHolder{
private TextView mTodoTitle;
private TextView mTodoDesc;

public ViewHolder(View itemView){
super(itemView);
mTodoTitle = (TextView) itemView.findViewById(R.id.tvTodoTitle);
mTodoDesc = (TextView) itemView.findViewById(R.id.tvTodoDesc);
}
}

public void addTodo(UserTodo userTodo){
mUserTodoRef.push().setValue(userTodo);
}

public void updateTodo(UserTodo userTodo, String newTodoTitle, String newTodoDesc){
userTodo.setTodoTitle(newTodoTitle);
userTodo.setTodoDesc(newTodoDesc);
mUserTodoRef.child(userTodo.getTodoKey()).setValue(userTodo);
}

public void removeTodo(UserTodo userTodo){
mUserTodoRef.child(userTodo.getTodoKey()).removeValue();
}
}

用户在AddUser Activity 中点击Next按钮后,用户数据直接添加到Firebase,用户将被重定向到ToDo页面,用户可以在其中添加待办事项。如何传递在AddUser中创建的push key,使得当用户添加待办事项时,该事项会添加到用户下?

使用 Intent 的方式是否正确?

请不要问我为什么需要让用户在创建用户后立即添加待办事项列表。需要这样。

谢谢

编辑:很抱歉,我应该提到应该将 Intent 传递给 UserTodoAdapter 类,以便在 UserTodoAdapter 的 Firebase 数据库引用中,我可以指向对从 AddUser 传递的 key 的引用。

我有类 UserDetailsUserTodo,分别用于 Activity AddUserToDo 以处理 Firebase 中的数据。

最终数据将如下所示:

{
"users":{
"push_id":{
"userid":"123456",
"username":"My User",
"todo_s":{
"push_id":{
"todo1":"Title1",
"todo_desc":"Description"
},
"push_id":{
"todo2":"Title2",
"todo_desc":"Description"
},
}
},
}
}

最佳答案

通过 Intent 传递(从 AddUserToDo)没问题。或者您可以将其保存到本地存储,例如 SharedPreferences,这样您的用户就不必在用户创建新用户时创建新用户。

要将 ToDo Activity 中的键值传递给适配器,请修改适配器的构造函数以接受键参数

public UserTodoAdapter(Callback callback, String key) {
mCallback = callback;
mUserTodo = new ArrayList<>();
mUserTodoRef = FirebaseDatabase.getInstance.getReference().child(users).child(key).child("todo");
}

ToDo 中,通过传递来自上一个 Activity (AddUser) 的字符串来实例化适配器。

mAdapter = new UserTodoAdapter(this, getIntent().getStringExtra("key"));

关于Android - 将 Intent 从一个 Activity 传递到另一个使用 Firebase 实现回调类的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38681972/

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