所以我在Firebase中使用RecyclerView,我得到了这个错误代码
我MainActivity.java
mDatabase.child("User").child(uid).child("Database").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot Snapshot) {
list.clear();
for (DataSnapshot dataSnapshot : Snapshot.getChildren()) {
Item item = dataSnapshot.getValue(Item.class);
list.add(item);
}
Collections.sort(list, (l1, l2) -> l1.getNama().compareTo(l2.getNama())); //buat sort alphabetically
itemAdapter = new ItemAdapter(MainActivity.this, list);
recyclerView.setAdapter(itemAdapter);
progressDialog.dismiss();
}
我Item.java
public class Item {
private String nama, harga, stok, satuan, limit;
public Item() {
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getHarga() {
return harga;
}
public void setHarga(String harga) {
this.harga = harga;
}
public String getStok() {
return stok;
}
public void setStok(String stok) {
this.stok = stok;
}
public String getSatuan() {
return satuan;
}
public void setSatuan(String satuan) {
this.satuan = satuan;
}
public String getLimit() {
return limit;
}
public void setLimit(String limit) {
this.limit = limit;
}
}
我的ItemAdapter.java
package pipapp.com.pipile.Adapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import pipapp.com.pipile.Menu.MainHoldMenu;
import pipapp.com.pipile.Menu.MainTouchMenu;
import pipapp.com.pipile.Model.Item;
import pipapp.com.pipile.R;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> implements Filterable {
Context context;
List<Item> itemList;
List<Item> itemTempList;
int limit, stok;
public ItemAdapter(Context context, List<Item> TempList) {
this.itemList = TempList;
this.context = context;
}
@NonNull
@Override
public ItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_row, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ItemAdapter.ViewHolder holder, int position) {
Item item = itemList.get(position);
holder.qty.setText(item.getStok());
holder.nama.setText(item.getNama());
holder.harga.setText("Rp. " + item.getHarga());
holder.satuan.setText(item.getSatuan());
limit = Integer.parseInt(item.getLimit());
stok = Integer.valueOf(item.getStok());
int statuscolor = 0;
if (stok <= limit) {
statuscolor = Color.parseColor("#f44242");
}
holder.onklik.setBackgroundColor(statuscolor);
holder.onklik.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, MainTouchMenu.class);
i.putExtra("nama", item.getNama().toString());
i.putExtra("stok", item.getStok().toString());
i.putExtra("harga", item.getHarga().toString());
i.putExtra("satuan", item.getSatuan().toString());
i.putExtra("limit", item.getLimit().toString());
context.startActivity(i);
}
});
holder.onklik.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Intent i = new Intent(context, MainHoldMenu.class);
i.putExtra("nama", item.getNama().toString());
i.putExtra("stok", item.getStok().toString());
i.putExtra("harga", item.getHarga().toString());
i.putExtra("satuan", item.getSatuan().toString());
i.putExtra("limit", item.getLimit().toString());
context.startActivity(i);
return false;
}
});
}
@Override
public int getItemCount() {
return itemList.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final List<Item> results = new ArrayList<Item>();
if (itemTempList == null) {
itemTempList = itemList;
}
if (constraint != null) {
if (itemTempList != null & itemTempList.size() > 0) {
for (final Item item : itemTempList) {
if (item.getNama().toLowerCase().contains(constraint.toString())) {
results.add(item);
}
}
}
oReturn.values = results;
}
return oReturn;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
itemList = (List<Item>) results.values;
notifyDataSetChanged();
}
};
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView nama, harga, qty, satuan;
RelativeLayout onklik;
public ViewHolder(View itemView) {
super(itemView);
nama = itemView.findViewById(R.id.nama);
harga = itemView.findViewById(R.id.harga);
qty = itemView.findViewById(R.id.qty);
onklik = itemView.findViewById(R.id.onklik);
satuan = itemView.findViewById(R.id.satuan);
}
}
}
这是Firebase结构
{
"User" : {
"NFZmA3cypLZproSk5yE2hEu1dx43" : {
"Database" : {
"harga" : "123123",
"limit" : "123123",
"nama" : "asdasd",
"satuan" : "asd",
"stok" : "12312"
},
"username" : "test"
}
}
}
试试这个:-
mDatabase.child("User").child(uid).child("Database").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot Snapshot) {
list.clear();
Item item = dataSnapshot.getValue(Item.class);
list.add(item);
Collections.sort(list, (l1, l2) -> l1.getNama().compareTo(l2.getNama())); //buat sort alphabetically
itemAdapter = new ItemAdapter(MainActivity.this, list);
recyclerView.setAdapter(itemAdapter);
progressDialog.dismiss();
}