问题描述
我试图从Firebase检索数据到Android项目上的recycler-view.首先,这是我在firebase上的结构数据库
i tried to retrieving data from Firebase to recycler-view on my Android project.first, here's my structure database on firebase
然后,我写Ph课
public class Ph {
public String date;
public String nilai;
public String time;
public Ph(){
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getNilai() {
return nilai;
}
public void setNilai(String nilai) {
this.nilai = nilai;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
为保存数据,我创建了PhAdapter类.
to save data, i create PhAdapter class.
public class PhAdapter extends RecyclerView.Adapter<PhAdapter.PhViewHolder> {
private ArrayList<Ph> dataList;
public int rowLayout;
private Context context;
public static class PhViewHolder extends RecyclerView.ViewHolder {
private TextView tvNilai, tvDate, tvTime;
private LinearLayout phLayout;
public PhViewHolder(View itemview) {
super(itemview);
phLayout = (LinearLayout) itemview.findViewById(R.id.ph_view);
tvNilai = (TextView) itemview.findViewById(R.id.tv_ph);
tvTime = (TextView) itemview.findViewById(R.id.tv_time);
tvDate = (TextView) itemview.findViewById(R.id.tv_date);
}
}
public PhAdapter(ArrayList<Ph> dataList, Context context) {
this.dataList = dataList;
this.context = context;
}
@Override
public PhAdapter.PhViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new PhViewHolder(view);
}
@Override
public void onBindViewHolder(final PhViewHolder holder, final int position) {
holder.tvNilai.setText(dataList.get(position).getNilai());
holder.tvTime.setText(dataList.get(position).getTime());
holder.tvDate.setText(dataList.get(position).getDate());
}
@Override
public int getItemCount() {
return dataList.size();
}
}
最后一个是我的MainActivity类
the last one, my MainActivity class
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
TextView txtNilai, txtDate, txtTime;
Button cekdata;
private DatabaseReference mDatabase;
//get reference database
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
cekdata = (Button)findViewById(R.id.btn_cekdata);
txtNilai = (TextView)findViewById(R.id.tv_ph);
txtDate = (TextView)findViewById(R.id.tv_date);
txtTime = (TextView)findViewById(R.id.tv_time);
//firebase
mDatabase = FirebaseDatabase.getInstance().getReference("https://raspi-ph.firebaseio.com/Fakultas Teknik/ph");
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
cekdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Ph ph = dataSnapshot.getValue(Ph.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
//Getting Ph failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
mDatabase.addValueEventListener(postListener);
}
});
}
}
但是,当我将项目运行到模拟器时,出现错误java.lang.NoSuchErrorMethod. MainActivity上第42行引起的错误是获取参考数据库的行.
but, when i run the project to emulator, i got error java.lang.NoSuchErrorMethod. the error cause by line 42 on MainActivity, is a line to get reference database.
请帮助我.我的代码有什么错误吗?
please help me. there's something error in my code?
错误消息:
推荐答案
此问题已解决.我从Firebase控制台下载google-service.json并将其添加到project/app文件夹:)
this question is solve. i downloading google-service.json from firebase console and add it to project/app folder :)
这篇关于我的Firebase项目出现错误java.lang.NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!