0

I'm showing data from a Firebase Firestore collection, the app worked fine while but when I update data to the collection from other device (I got an Arduino with sensors connected to a PC that executes a Python script to transform the serial data to JSON and then I update that data on the Firestore collection, all the back end of these functionality works perfectly. My problem it's the Java on Android.

I already search for solutions on this forum and It seems like something doesn't work with the Adapter, the RecyclerView and the "notifyDataSetChanged();" None of the current solutions worked for me or maybe I just don't know how to implement them on my project.

This is my model

public class Monitor {
    String alias, placa, temp, acid;

    public Monitor(){}

    public Monitor(String alias, String placa, String temp, String acid) {
        this.alias = alias;
        this.placa = placa;
        this.temp = temp;
        this.acid = acid;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getPlaca() {
        return placa;
    }

    public void setPlaca(String placa) {
        this.placa = placa;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public String getAcid() {
        return acid;
    }

    public void setAcid(String acid) {
        this.acid = acid;
    }
}

The adapter

public class MonitorAdapter extends FirestoreRecyclerAdapter<Monitor, MonitorAdapter.ViewHolder> {
    private FirebaseFirestore mFirestore = FirebaseFirestore.getInstance();
    Activity activity;
    /**
     * Create a new RecyclerView adapter that listens to a Firestore Query.  See {@link
     * FirestoreRecyclerOptions} for configuration options.
     *
     * @param options
     */
    public MonitorAdapter(@NonNull FirestoreRecyclerOptions<Monitor> options, Activity activity) {
        super(options);
        this.activity = activity;
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Monitor model) {
        DocumentSnapshot documentSnapshot = getSnapshots().getSnapshot(holder.getAbsoluteAdapterPosition());
        final String id = documentSnapshot.getId();
        holder.alias.setText(model.getAlias());
        holder.temp.setText(model.getTemp());
        holder.acid.setText(model.getAcid());

        holder.btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(activity, VincularPlaca.class);
                i.putExtra("id_placa",id);
                activity.startActivity(i);
            }
        });

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_monitor_single,parent,false);
        return new ViewHolder(view);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView alias, temp, acid;
        Button btn_edit;

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

            alias = itemView.findViewById(R.id.alias);
            temp = itemView.findViewById(R.id.temp);
            acid = itemView.findViewById(R.id.acid);
            btn_edit = itemView.findViewById(R.id.btn_edit);

        }
    }

}

And my MainActivity

public class MainActivity extends AppCompatActivity {

    Button btn_add, btn_exit;
    RecyclerView mRecycler;
    MonitorAdapter mAdapter;
    FirebaseFirestore mFirestore;
    FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFirestore = FirebaseFirestore.getInstance();
        mRecycler = findViewById(R.id.recyclerViewSingle);
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        Query query = mFirestore.collection("dispositivos");

        FirestoreRecyclerOptions<Monitor> firestoreRecyclerOptions =
                new FirestoreRecyclerOptions.Builder<Monitor>().setQuery(query,Monitor.class).build();

        mAdapter = new MonitorAdapter(firestoreRecyclerOptions, this);

        mAdapter.notifyDataSetChanged();
        mRecycler.setAdapter(mAdapter);

        btn_add = findViewById(R.id.btn_add);
        btn_exit = findViewById(R.id.btn_close);

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,VincularPlaca.class));
            }
        });

        btn_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,LoginActivity.class));
                mAuth.signOut();
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        mAdapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mAdapter.stopListening();
    }

}
Laurel
  • 5,965
  • 14
  • 31
  • 57
  • There's no stack-trace, which means the question is insufficient. Calling `notifyDataSetChanged()` on a new instance of an adapter appears strange to me, as it provides no meaning. – Martin Zeitler Nov 29 '22 at 05:20
  • If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question. Please respond using @AlexMamo – Alex Mamo Nov 29 '22 at 05:23
  • @AlexMamo my Android Studio device manager and emulator is not working correctly so I just build the apk and execute on a physichal device, so I got not access to the logcat when this error happens, but the "crash" it just the app closing itself when I upload data to the collection on firestore. Looking for solutions I found some way to do the document snapshot on another void, related to some "refreshing button" or something, that reads the data again, but also I would have change the recyclerview to a "listview". I don't have experiencie with these stuff I apologize. – RodrigoMardones Nov 29 '22 at 06:22
  • Why have you written mAdapter.notifyDataSetChanged() when you are using mAdapter.startListening() and mAdapter.stopListening() ?. Doesn't make sense – REX Nov 29 '22 at 13:32

2 Answers2

0
class ExampleViewModel : ViewModel() {
var mFirestore : FirebaseFirestore? = null
private val _list = MutableLiveData<FirestoreRecyclerOptions<Monitor>>()
val list: LiveData<FirestoreRecyclerOptions<Monitor>> = _list

init{
    mFirestore = FirebaseFirestore.getInstance()
    list.value= FirestoreRecyclerOptions.Builder<Monitor>().setQuery(mFirestore!!.collection("dispositivos"),Monitor::class.java).build()
}

}

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 03 '22 at 02:30
0

The problem has solved fixing the AndroidManifest Permissions and outside the code on the Firebase Firestore console, the attribute was supose to be an string but it recieves an int.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 27 '23 at 06:43