我有一个活动与数据库连接并且我所有的数据都来了,但是我想当我单击一个Card视图以从数据卡中打开数据时。 我喜欢第一页的类别,当我单击一个类别时,我想获取该类别中的所有项目。 我仍在(独自)学习,这是我自己无法解决的第一个问题。 请帮我。 我已经尝试了所有可以在网上找到的东西。
public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference firemRef = db.collection("kategorije");
private FirmeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpRecyclerView();
}
private void setUpRecyclerView(){
Query query= firemRef.orderBy("logo",Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Firme> options = new
FirestoreRecyclerOptions.Builder<Firme>()
.setQuery(query,Firme.class)
.build();
adapter= new FirmeAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
public void onClickButton (View view){
Intent intent= new Intent(MainActivity.this,BeautyIzbornik.class);
MainActivity.this.startActivity(intent);
}
}
我的适配器
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.squareup.picasso.Picasso;
public class FirmeAdapter extends FirestoreRecyclerAdapter<Firme, FirmeAdapter.FirmeHolder> {
/**
* Create a new RecyclerView adapter that listens to a Firestore Query. See {@link
* FirestoreRecyclerOptions} for configuration options.
*
* @param options
*/
public FirmeAdapter(@NonNull FirestoreRecyclerOptions<Firme> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull FirmeHolder holder, int position, @NonNull final Firme model) {
holder.naziv.setText(model.getNaziv());
holder.setLogo(model.getLogo());
}
@NonNull
@Override
public FirmeHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.firme_item,
viewGroup,false);
return new FirmeHolder(v);
}
class FirmeHolder extends RecyclerView.ViewHolder {
ImageButton imageButton;
TextView naziv;
public FirmeHolder(@NonNull final View itemView) {
super(itemView);
naziv = itemView.findViewById(R.id.naziv);
imageButton =itemView.findViewById(R.id.logo);
}
public void setLogo(String logo) {
Picasso.get().load(logo).into(imageButton);
}
}
}
我的活动和数据
public class Firme {
public String logo;
public String naziv;
public String url;
public Firme ( ) {
}
public Firme (String naziv, String logo , String url){
this.logo= logo;
this.naziv= naziv;
this.url = url;
}
public String getLogo() {
return logo;
}
public String getNaziv() {
return naziv;
}
public void setLogo(String logo){
this.logo = logo;
}
public String getUrl(){
return url;
}
public void setUrl(String url){
this.url=url;
}
}
答案 0 :(得分:0)
您必须对RecyclerView
项执行点击侦听器:
由于每个项目都有自己的数据,因此您应该使用此数据显示足够的信息:
如何为RecyclerView
在您的RecyclerView.Adapter
中,只需创建一个可以使用的界面即可。
private onItemClickListener mItemClickListener;
public void setOnItemClickListener(onItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public interface onItemClickListener {
void onItemClickListener(View view, int position, MyData myData);
}
在这种情况下,mItemClickListener是适配器内部的全局变量,并且这两种方法也都位于适配器内部。
然后在ViewHolder内部,使用itemView.setOnClickListener(this)(或您要单击以检索数据的任何组件)将click侦听器应用于itemView
@Override
public void onClick(View view) {
if (mItemClickListener != null) {
mItemClickListener.onItemClickListener(view, getAdapterPosition(), my_data.get(getAdapterPosition()));
}
}
这将通过已单击的视图,该视图的位置以及该位置内列表中包含的对象。
因此,在“活动”中,您只需简单地调用界面即可。
adapter.setOnItemClickListener(new CustomAdapter.onItemItemClickListener() {
@Override
public void onItemClickListener(View view, int position, MyData data) {
//do on item click functionality here
}
});