如何在单击列表视图时将多个值传递给 firebase
how to pass multiple value to firebase on click of listview
我想在点击列表视图中的项目时将数据存储在 firebase 中。
我的列表视图 activity 如下所示。谁能帮忙。我是 android 工作室的新手
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) {
}
});
}
}
或者是否有任何其他方法可以使用复选框或其他方式选择动态打印的项目并存储在 firebase databse.Like 中...
感谢任何形式的帮助:)
试试这个
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));
}
});
我想在点击列表视图中的项目时将数据存储在 firebase 中。 我的列表视图 activity 如下所示。谁能帮忙。我是 android 工作室的新手
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) {
}
});
}
}
或者是否有任何其他方法可以使用复选框或其他方式选择动态打印的项目并存储在 firebase databse.Like 中... 感谢任何形式的帮助:)
试试这个
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));
}
});