来自Firebase的数据未在片段中的RecyclerView中显示。

huangapple go评论81阅读模式
英文:

Data from Firebase is not displayed in the RecyclerView that is in the fragment

问题

有一个问题当我运行程序时在RecyclerView中显示的数据有问题

它看起来像这样
[图片1][1]

对于显示的数据我使用了类似于这样的Firebase数据结构
[图片2][2]

当我想要在一个片段中显示RecyclerView中的数据时数据没有显示出来我使用Firebase作为数据库

NotaAdapter.java

    public class NotaAdapter extends RecyclerView.Adapter<NotaAdapter.MyViewHolder> {

    Context context;
    ArrayList<ListNota> listnota;

    public NotaAdapter(Context c, ArrayList<ListNota> p) {
        this.context = c;
        this.listnota = p;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        return new MyViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.item_nota, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        myViewHolder.no_nota.setText(listnota.get(i).getId_nota());
        myViewHolder.total_harga.setText(String.valueOf(listnota.get(i).getTotal_seluruh()));

        final String getnoNOta = listnota.get(i).getId_nota();

        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent gotoDetailNota = new Intent(context, DetailNotaAct.class);
                gotoDetailNota.putExtra("no_nota", getnoNOta);
                context.startActivity(gotoDetailNota);
            }
        });
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView no_nota, total_harga;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            no_nota = itemView.findViewById(R.id.xid_nota);
            total_harga = itemView.findViewById(R.id.xtotal_seluruh);
        }
    }
}

ListNota.java

public class ListNota {
    private String id_nota;
    private Integer total_seluruh;

    public ListNota() {

    }

    public ListNota(String id_nota, Integer total_seluruh) {
        this.id_nota = id_nota;
        this.total_seluruh = total_seluruh;
    }

    public String getId_nota() {
        return id_nota;
    }

    public void setId_nota(String id_nota) {
        this.id_nota = id_nota;
    }

    public Integer getTotal_seluruh() {
        return total_seluruh;
    }

    public void setTotal_seluruh(Integer total_seluruh) {
        this.total_seluruh = total_seluruh;
    }
}

HistoryFragment.java

public class HistoryFragment extends Fragment {

    TextView txt_history, txt_toko, txt_report, txt_nama_toko, txt_jenis_toko;
    LinearLayout btn_buat_nota;

    DatabaseReference databaseUser, databaseToko, databaseNota;

    String USERNAME_KEY = "usernamekey";
    String username_key = "";
    String username_key_new = "";
    String id_Toko = "";

    ProgressDialog progress;

    RecyclerView nota_place;
    ArrayList<ListNota> list;
    NotaAdapter notaAdapter;

    private View Notaview;

    public HistoryFragment(){

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Notaview = inflater.inflate(R.layout.fragment_history, container, false);

        txt_nama_toko = (TextView) Notaview.findViewById(R.id.txt_nama_toko);
        txt_jenis_toko = (TextView) Notaview.findViewById(R.id.txt_jenis_toko);
        txt_history = (TextView) Notaview.findViewById(R.id.txt_history);
        txt_toko = (TextView) Notaview.findViewById(R.id.txt_toko);
        txt_report = (TextView) Notaview.findViewById(R.id.txt_report);
        btn_buat_nota = (LinearLayout) Notaview.findViewById(R.id.btn_buat_nota);

        progress = new ProgressDialog(getActivity());
        progress.setTitle("Loading");
        progress.setMessage("Memuat Data");
        progress.setCancelable(false);
        progress.show();

        getUsernameLocal();

        databaseUser = FirebaseDatabase.getInstance().getReference().child("Users").child(username_key_new);
        databaseUser.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                id_Toko = dataSnapshot.child("id_toko").getValue().toString();

                databaseToko = FirebaseDatabase.getInstance().getReference().child("Toko").child(id_Toko);
                databaseToko.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        txt_nama_toko.setText(dataSnapshot.child("nama_toko").getValue().toString());

                        if (dataSnapshot.hasChild("jenis_toko")){
                            txt_jenis_toko.setText(dataSnapshot.child("jenis_toko").getValue().toString());
                        }else{
                            txt_jenis_toko.setText("Jenis toko belum disetting");
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        btn_buat_nota.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String id_nota = generateRandomString(16);
                Intent gotoBuatNota = new Intent(getActivity(), BuatNotaAct.class);
                gotoBuatNota.putExtra("id_nota", id_nota);
                startActivity(gotoBuatNota);
            }
        });

        nota_place = (RecyclerView) Notaview.findViewById(R.id.nota_place);
        notaAdapter = new NotaAdapter(getContext(), list);
        nota_place.setAdapter(notaAdapter);
        nota_place.setLayoutManager(new LinearLayoutManager(getActivity()));
        return Notaview;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = new ArrayList<ListNota>();
        loaddata();
    }

    private void loaddata(){
        databaseNota = FirebaseDatabase.getInstance().getReference().child("Nota").child(id_Toko);
        databaseNota.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
                    ListNota p = dataSnapshot1.getValue(ListNota.class);
                    list.add(p);
                }
                progress.dismiss();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    public String generateRandomString(int length){
        char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for(int i = 0; i < length; i++){
            char c = chars[random.nextInt(chars.length)];
            stringBuilder.append(c);
        }
        return stringBuilder.toString();
    }

    public void getUsernameLocal(){
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences(USERNAME_KEY, MODE_PRIVATE);
        username_key_new = sharedPreferences.getString(username_key, "");
    }
}
英文:

There is a problem with the data displayed in the recyclerview when I run my program

it looks like this:
来自Firebase的数据未在片段中的RecyclerView中显示。.

For the data that is displayed I use firebase like this the

data structure:来自Firebase的数据未在片段中的RecyclerView中显示。

When I want to display data in recyclerview in a fragment, but the data doesn't appear. I use Firebase as database

NotaAdapter.java

public class NotaAdapter extends RecyclerView.Adapter&lt;NotaAdapter.MyViewHolder&gt; {
Context context;
ArrayList&lt;ListNota&gt; listnota;
public NotaAdapter (Context c,ArrayList&lt;ListNota&gt; p) {
this.context = c;
this.listnota = p;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
return new MyViewHolder(LayoutInflater.from(context)
.inflate(R.layout.item_nota, parent, false));
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.no_nota.setText(listnota.get(i).getId_nota());
myViewHolder.total_harga.setText(String.valueOf(listnota.get(i).getTotal_seluruh()));
final String getnoNOta = listnota.get(i).getId_nota();
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent gotoDetailNota = new Intent(context, DetailNotaAct.class);
gotoDetailNota.putExtra(&quot;no_nota&quot;, getnoNOta);
context.startActivity(gotoDetailNota);
}
});
}
@Override
public int getItemCount() {
return listnota.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView no_nota, total_harga;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
no_nota = itemView.findViewById(R.id.xid_nota);
total_harga = itemView.findViewById(R.id.xtotal_seluruh);
}
}
}

ListNota.java

public class ListNota {
private String id_nota;
private Integer total_seluruh;
public ListNota() {
}
public ListNota(String id_nota, Integer total_seluruh) {
this.id_nota = id_nota;
this.total_seluruh = total_seluruh;
}
public String getId_nota() {
return id_nota;
}
public void setId_nota(String id_nota) {
this.id_nota = id_nota;
}
public Integer getTotal_seluruh() {
return total_seluruh;
}
public void setTotal_seluruh(Integer total_seluruh) {
this.total_seluruh = total_seluruh;
}
}

HistoryFragment.java

public class HistoryFragment extends Fragment {
TextView txt_history, txt_toko, txt_report, txt_nama_toko, txt_jenis_toko;
LinearLayout btn_buat_nota;
DatabaseReference databaseUser, databaseToko, databaseNota;
String USERNAME_KEY = &quot;usernamekey&quot;;
String username_key = &quot;&quot;;
String username_key_new = &quot;&quot;;
String id_Toko = &quot;&quot;;
ProgressDialog progress;
RecyclerView nota_place;
ArrayList&lt;ListNota&gt; list;
NotaAdapter notaAdapter;
private View Notaview;
public HistoryFragment(){
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Notaview = inflater.inflate(R.layout.fragment_history, container, false);
txt_nama_toko = (TextView) Notaview.findViewById(R.id.txt_nama_toko);
txt_jenis_toko = (TextView) Notaview.findViewById(R.id.txt_jenis_toko);
txt_history = (TextView) Notaview.findViewById(R.id.txt_history);
txt_toko = (TextView) Notaview.findViewById(R.id.txt_toko);
txt_report = (TextView) Notaview.findViewById(R.id.txt_report);
btn_buat_nota = (LinearLayout) Notaview.findViewById(R.id.btn_buat_nota);
progress = new ProgressDialog(getActivity());
progress.setTitle(&quot;Loading&quot;);
progress.setMessage(&quot;Memuat Data&quot;);
progress.setCancelable(false);
progress.show();
getUsernameLocal();
databaseUser = FirebaseDatabase.getInstance().getReference().child(&quot;Users&quot;).child(username_key_new);
databaseUser.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
id_Toko = dataSnapshot.child(&quot;id_toko&quot;).getValue().toString();
databaseToko = FirebaseDatabase.getInstance().getReference().child(&quot;Toko&quot;).child(id_Toko);
databaseToko.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
txt_nama_toko.setText(dataSnapshot.child(&quot;nama_toko&quot;).getValue().toString());
//cek apakah child jenis toko ada
if (dataSnapshot.hasChild(&quot;jenis_toko&quot;)){
txt_jenis_toko.setText(dataSnapshot.child(&quot; jenis_toko&quot;).getValue().toString());
}else{
txt_jenis_toko.setText(&quot;Jenis toko belum disetting&quot;);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
btn_buat_nota.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String id_nota = generateRandomString(16);
Intent gotoBuatNota = new Intent(getActivity(), BuatNotaAct.class);
gotoBuatNota.putExtra(&quot;id_nota&quot;, id_nota);
startActivity(gotoBuatNota);
}
});
nota_place = (RecyclerView) Notaview.findViewById(R.id.nota_place);
notaAdapter = new NotaAdapter(getContext(), list);
nota_place.setAdapter(notaAdapter);
nota_place.setLayoutManager(new LinearLayoutManager(getActivity()));
return Notaview;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = new ArrayList&lt;ListNota&gt;();
loaddata();
}
private void loaddata(){
databaseNota = FirebaseDatabase.getInstance().getReference().child(&quot;Nota&quot;).child(id_Toko);
databaseNota.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
ListNota p = dataSnapshot1.getValue(ListNota.class);
list.add(p);
}
progress.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public String generateRandomString(int length){
char[] chars = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;.toCharArray();
StringBuilder stringBuilder = new StringBuilder();
Random random = new Random();
for(int i = 0; i &lt; length; i++){
char c = chars[random.nextInt(chars.length)];
stringBuilder.append(c);
}
return stringBuilder.toString();
}
public void getUsernameLocal(){
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(USERNAME_KEY, MODE_PRIVATE);
username_key_new = sharedPreferences.getString(username_key,&quot;&quot;);
}
}

答案1

得分: 0

你正在从错误的节点读取它,你正在传递一个引用,你想从名为“users”的节点中读取数据。

你的数据库引用应该像这样:

databaseUser = FirebaseDatabase.getInstance().getReference("Nota").child("Toko_Kita20082020371").child(username_key_new);

同时确保你的列表应该包含一个节点拥有的所有变量。
例如,一个节点有

name、id、price、description;

那么你的列表中也应该声明相同的变量,以成功地从 Firebase 中读取数据。

英文:

you are reading it from the wrong node, you are passing reference that you want to read data from the node Named "users".

Your Database reference should look like this

databaseUser = FirebaseDatabase.getInstance().getReference(&quot;Nota&quot;).child(&quot;Toko_Kita20082020371&quot;).child(username_key_new);

Also make sure your list should have all the variables that a node has.
For example a node has

name,id,price,description;

then same varibles should be declared in your list to successfully ready dta from the firebase.

huangapple
  • 本文由 发表于 2020年9月28日 03:13:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64092344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定