In my app I have a button which makes a call to firebase to locate certain keys with their respective values, which will be added to an Array which will be shown in a Listview.
When entering a value in the EditText
followed by pressing the button that makes consultation with Firebase, show me proper records correctly, the problem is that if I press the button disappears ListView
.
I'm working on a Fragment, I think the problem is that at last establish that the Array is cleaned so that when you press the button to add other records depending on what you enter in the EditText
but what happens is that it disappears.
This is my code:
private void fragment_rastreo(){
mEstado = (TextView) rootView.findViewById(R.id.estado);
mEstado2 = (TextView) rootView.findViewById(R.id.estado2);
E_buscar = (EditText)rootView.findViewById(R.id.E_buscar);
B_buscar = (Button)rootView.findViewById(R.id.B_buscar);
mProductosList = (ListView) rootView.findViewById(R.id.productos_list);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContexto, android.R.layout.simple_list_item_1, mProductos);
mProductosList.setAdapter(arrayAdapter);
final String TAG = "RastreoFragment.java";
B_buscar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String buscar = E_buscar.getText().toString().trim();
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
if (postSnapshot.getKey().equals(buscar)){
Log.d(TAG, "Key: " + postSnapshot.getKey());
Log.d(TAG, "Value: " + postSnapshot.getValue());
mEstado.setText("\nEl numero #" + buscar + " contiene: " + postSnapshot.getValue() + "\n");
mDatabase2 = mDatabase2.child(buscar);
mDatabase2.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
mEstado2.setText("\nLo que contiene este numero es:\n ");
String value = dataSnapshot.getKey() + " " + dataSnapshot.getValue();
mProductos.add(value);
arrayAdapter.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) {
}
});
break;
}else{
if (buscar.equals("")){
mEstado.setText("\nIngrese un numero valido.\n");
}else {
mEstado.setText("\nEl numero #" + buscar + " no tiene registro.\n");
}
}
}
mProductos.clear();
mEstado2.setText("");
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
I had done this in an app without fragment and it worked correctly but when I passed the code to a Fragment, this error appeared, disappearing the ListView
.
Thank you.