这是我的recyclerview课,其中有电工,水管工,焊工等作为我的recyclerview项。我希望每当用户单击这些项目中的任何一项时,从Firebase检索的相应数据都应在下一个活动中显示。
package com.example.hp.dc_project;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/** * Created by HP on 20-07-2018. */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context mContext;
private List<category> mdata;
public RecyclerViewAdapter(Context context, List<category> mdata) {
this.mContext = context;
this.mdata = mdata;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(mContext);
View view = mInflater.inflate(R.layout.cardview_category,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv.setText(mdata.get(position).getName());
holder.iv.setImageResource(mdata.get(position).getThumnail());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mContext.getApplicationContext(),available_worker.class);
mContext.startActivity(i);
}
});
}
@Override
public int getItemCount() {
return mdata.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv;
ImageView iv;
CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
tv = (TextView)itemView.findViewById(R.id.tv_category);
iv = (ImageView)itemView.findViewById(R.id.iv_category);
cardView = (CardView)itemView.findViewById(R.id.cardview);
}
}
}
这是活动类,我要在其中显示包含在单击recyclerview项目后从firebase检索到的数据的列表视图:
package com.example.hp.dc_project;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
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;
import java.util.ArrayList;
public class available_worker extends AppCompatActivity {
private ListView lv;
private FirebaseDatabase database;
private ArrayList<String>list;
private ArrayAdapter<String>adapter;
private Electrician elec;
// adapter = new ArrayAdapter<Electrician>(this,R.layout.list_of_work
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_available_worker);
lv = (ListView)findViewById(R.id.listView_workeravailable);
elec = new Electrician();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?>parent, View view, int position, long id) {
Intent i = new Intent(available_worker.this,WorkerProfile.class);
startActivity(i);
}
});
list = new ArrayList<>();
adapter = new ArrayAdapter<String>(this, R.layout.list_of_worker,R.id.employeeadd);
database = FirebaseDatabase.getInstance();
DatabaseReference rootref = database.getReference();
DatabaseReference electricianref = rootref.child("Employee").child("Electrician");
electricianref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String fName = ds.child("Fname").getValue(String.class);
String lName = ds.child("Lname").getValue(String.class);
String address = ds.child("Address").getValue(String.class);
list.add(fName);
list.add(lName);
list.add(address);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
答案 0 :(得分:0)
在单击时,获取您的工作人员id并放入extra,在下一个活动中放置value事件列表器并获取相应的数据,但是您有一件事是您必须具有id或可以唯一标识引用的对象火力基地。
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mContext.getApplicationContext(),available_worker.class);
i.putExtra("uid",workerid);
mContext.startActivity(i);
}
});
在下一个活动中
String s = getIntent().getStringExtra("uid");
firebaseref.child(s).addvalueEventListner(////) {//pupulate your list view}