gpt4 book ai didi

android - 将从 firebase 检索到的键值添加到 arraylist 不起作用

转载 作者:行者123 更新时间:2023-11-29 19:13:16 25 4
gpt4 key购买 nike

我正在使用 recyclerview 显示来自 firebase 的数据。我使用数组列表存储它们来自 firebase 的键值,然后使用该数组列表填充回收器 View ,但这里的问题是键值没有添加到列表中,但是当我使用 toast 显示键时重视它的工作正常。我犯了什么错误,我该如何解决?

package com.sabari.inventory;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.firebase.database.ChildEventListener;
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 java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements MyAdapter.ClickListener{

RecyclerView recyclerView;
MyAdapter adapter;
List<Info> infos;
DatabaseReference myRef,dr;
List<String> tit,d;
int position;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.myRecView);
tit = new ArrayList<>();
d = new ArrayList<>();

myRef = FirebaseDatabase.getInstance().getReference();
dr = myRef.child("Machines");
adapter = new MyAdapter(getApplicationContext(),getData());
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

}

public List<Info> getData(){

infos = new ArrayList<>();
dr.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
tit.add(dataSnapshot.getKey());
adapter.notifyDataSetChanged();
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

for(int i=0;i<tit.size();i++) {
Info now = new Info();
now.title = tit.get(i);
now.desc = "";
infos.add(now);
}return infos;
}

@Override
public void itemClicked(View view, int position) {
MyDialog myDialog = new MyDialog();
myDialog.show(getSupportFragmentManager(),"");

}
}

信息类

package com.sabari.inventory;



public class Info {

int img;
String title;
String desc;
}

适配器类

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyVH>  {

LayoutInflater layoutInflater;
Context context;
ClickListener clickListener;
List<Info> infoList = Collections.emptyList();
public MyAdapter(Context context,List<Info> infoList) {
layoutInflater=LayoutInflater.from(context);
this.infoList = infoList;
this.context=context;
}

@Override
public MyVH onCreateViewHolder(ViewGroup parent, int viewType) {
View view=layoutInflater.inflate(R.layout.single_row,parent,false);
MyVH myVH = new MyVH(view);
return myVH;
}

@Override
public void onBindViewHolder(MyVH holder, final int position) {
Info now = infoList.get(position);
holder.textView1.setText(now.title);
holder.textView2.setText(now.desc);
}

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

public void setClickListener(ClickListener clickListener)
{
this.clickListener=clickListener;
}



class MyVH extends RecyclerView.ViewHolder implements View.OnClickListener{

ImageView imageView;
TextView textView1,textView2;
public MyVH(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
textView1 = (TextView) itemView.findViewById(R.id.textView);
textView2 = (TextView) itemView.findViewById(R.id.textView2);

itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if(clickListener!=null)
{
clickListener.itemClicked(view,getPosition());
}
}
}

public interface ClickListener{
public void itemClicked(View view,int position);

}
}

修改 getData() 方法

 public List<Info> getData(){

infos = new ArrayList<>();

dr.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Info info= dataSnapshot.getValue(Info.class);
tit.add(info.getTitle());
adapter.notifyDataSetChanged();
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

for(int i=0;i<1;i++) {
Info now = new Info();
now.title = tit.get(i);
now.desc = "";
infos.add(now);
}return infos;
}

最佳答案

将您的 Info.class 创建为对象:

public class Info {

int img;
String title;
String desc;

public Info() {};

public Info(int img, String title, String desc) {
this.img = img;
this.title = title;
this.desc = desc;
}
public int getImg() { return img; }
public String getTitle() { return title; }
public String getDesc() { return desc; }
}

在你的监听器中

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Info info = dataSnapshot.getValue(Info.class);
tit.add(info.getTitle());
adapter.notifyDataSetChanged();
}

关于android - 将从 firebase 检索到的键值添加到 arraylist 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44481559/

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