javaandroidfirebaseandroid-listviewonitemselectedlistener

how to pass multiple value to firebase on click of listview


I want to store data in firebase on click of items in listview. My List View activity is as below. Can anyone please help. I'am new to android studio

package android.example.mentoring_app;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class secondSem extends AppCompatActivity {
    ListView Lview;
    ArrayList<String> Alist=new ArrayList<>();
    DatabaseReference mRef;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_sem);
        ArrayAdapter<String> ad=new ArrayAdapter<>(secondSem.this, android.R.layout.simple_list_item_1,Alist);
        Lview=findViewById(R.id.scndl);
        Lview.setAdapter(ad);
        mRef= FirebaseDatabase.getInstance().getReference("students").child("2nd sem");
        mRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull @NotNull DataSnapshot snapshot, @Nullable @org.jetbrains.annotations.Nullable String previousChildName) {
                String value=snapshot.getValue(String.class);
                Alist.add(value);
                ad.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(@NonNull @NotNull DataSnapshot snapshot, @Nullable @org.jetbrains.annotations.Nullable String previousChildName) {
                ad.notifyDataSetChanged();
            }

            @Override
            public void onChildRemoved(@NonNull @NotNull DataSnapshot snapshot) {

            }

            @Override
            public void onChildMoved(@NonNull @NotNull DataSnapshot snapshot, @Nullable @org.jetbrains.annotations.Nullable String previousChildName) {

            }

            @Override
            public void onCancelled(@NonNull @NotNull DatabaseError error) {

            }
        });
    }
}

Or is there any other method to choose dynamically printed items and store in firebase databse.Like using checkbox or something... any kind of help is appreciated :)


Solution

  • try this

    Lview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // on listView item clicked
                myRef.setValue(ad.getItem(position));
            }
        });