我有一个带有问题的文字的recyclerview。我想做的是当用户点击 问题的预期答案应在下一视图的文本视图中显示。
这是我的数据库架构。
这是我的家碎片。
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.solver.widgets.Snapshot;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.firebase.ui.database.SnapshotParser;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import java.util.Objects;
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
FloatingActionButton addButton;
DatabaseReference mDatabase;
FirebaseRecyclerAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View RootView = inflater.inflate(R.layout.fragment_home, container, false);
addButton = RootView.findViewById(R.id.add);
recyclerView = RootView.findViewById(R.id.my_recycler_view);
mDatabase = FirebaseDatabase.getInstance().getReference("Question");
mDatabase.keepSynced(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
Query query = FirebaseDatabase.getInstance().getReference().child("Question");
FirebaseRecyclerOptions<Problem_Questions_RecyclerView> options = new FirebaseRecyclerOptions.Builder<Problem_Questions_RecyclerView>()
.setQuery(query, new SnapshotParser<Problem_Questions_RecyclerView>() {
@NonNull
@Override
public Problem_Questions_RecyclerView parseSnapshot(@NonNull DataSnapshot snapshot) {
return new Problem_Questions_RecyclerView(Objects.requireNonNull(snapshot.child("ID").getValue()).toString(),
Objects.requireNonNull(snapshot.child("Question").getValue()).toString());
}
}).build();
adapter = new FirebaseRecyclerAdapter<Problem_Questions_RecyclerView, ViewHolder>(options){
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.questions_recycler_view, parent, false);
return new ViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull ViewHolder viewHolder, int i, @NonNull Problem_Questions_RecyclerView details) {
viewHolder.setQuestion(details.getQuestion());
viewHolder.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Answers()).addToBackStack(null).commit();
}
});
}
};
recyclerView.setAdapter(adapter);
return RootView;
}
public class ViewHolder extends RecyclerView.ViewHolder{
LinearLayout root;
TextView Question;
ViewHolder(@NonNull View itemView) {
super(itemView);
root = itemView.findViewById(R.id.list_root);
Question = itemView.findViewById(R.id.txt);
}
void setQuestion(String string)
{
Question.setText(string);
}
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}
import android.app.DownloadManager;
public class Problem_Questions_RecyclerView {
public Problem_Questions_RecyclerView(){
}
public void setUID(String UID) {
this.UID = UID;
}
public String getQuestion() {
return Question;
}
public void setLocation(String question) {
Question = question;
}
public Problem_Questions_RecyclerView(String UID, String question) {
this.UID = UID;
Question = question;
}
private String UID,Question;
}
这是我的答案类,其中包含用于显示答案的文本视图。 我已经实现了类似的功能,当我单击它时,它应该显示问题本身而不是答案(测试目的),但是我必须给常量ID以获取数据。我该怎么做才能获得点击的recyclerview的ID并在下一个片段中显示答案
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class Answers extends Fragment {
DatabaseReference mDatabase;
private TextView textView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.answers, container, false);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Question").child("xxyy").child("Question");
textView = view.findViewById(R.id.answer);
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String v = dataSnapshot.getValue().toString();
textView.setText(v);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}
}
现在,当我单击具有
的回收站视图时question 2
它应该打开另一个包含
的片段this is answer 2
答案 0 :(得分:0)
您需要执行以下操作。在模型类中,您应该添加另一个包含答案的String。将答案存储在模型类中后,可以在适配器方法onBindViewHolder上的click方法上执行所需的操作,并在新活动中传递所需的值(您可以打包所有模型类,也可以只打包通过您需要的值,您的选择)。