我有以下firebase数据库结构: Firebase database structure 。此外,我有一个列表视图,显示所有不同的旅行。当我点击旅行时,我想将“TripID”子项作为字符串。我怎么能这样做?
我尝试使用以下代码,但我无法使其正常工作。
提前谢谢!
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
final DatabaseReference tripsRef = rootRef.child("Trips");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> listTime = new ArrayList<String>();
ArrayAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, listTime);
final ListView Time = (ListView) findViewById(R.id.Listtime);
for (final DataSnapshot ds : dataSnapshot.getChildren()) {
String user = ds.child("UserName").getValue(String.class);
String arrival = ds.child("Arrival").getValue(String.class);
String departure = ds.child("Departure").getValue(String.class);
String time = ds.child("Time").getValue(String.class);
final Boolean TripID = ds.child("TripID").getValue(boolean.class);
String Trips = TextUtils.join(" | ", new String[]{time, user, departure, arrival});
listTime.add(Trips);
Time.setAdapter(adapter);
Time.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(AvailableRides.this,RideSelected.class);
String entry = (String) adapterView.getItemAtPosition(i);
TextView textView4 = findViewById(R.id.textView30);
String user = textView4.getText().toString();
String selectedFromList =(String) (Time.getItemAtPosition(TripID));
intent.putExtra(EXTRA_MESSAGE10,user);
intent.putExtra(EXTRA_MESSAGE8, entry);
intent.putExtra(EXTRA_MESSAGE9, TripID);
startActivity(intent);
}
});
}
答案 0 :(得分:1)
要解决此问题,请在List<String> idsList = new ArrayList<>();
列表后面另行声明,如下所示:
final Boolean TripID = ds.child("TripID").getValue(boolean.class);
在for循环中,更改以下代码行:
String TripID = ds.child("TripID").getValue(String.class);
与
idsList.add(TripID);
此外,同样在for循环中,添加以下代码行:
String TripID = ds.child("TripID").getValue(String.class);
在此更改后的行之后:
setOnItemClickListener()
然后在String selectedFromList =(String) (Time.getItemAtPosition(TripID));
内,更改此行:
String TripID = idsList.get(position);
与
TripID
不要忘记在其他活动和代码的任何其他部分使用String
变量作为Boolean
,因为它在您的数据库中并且不是作为class MyClass
{
private:
atomic_int a;
thread t;
public:
MyClass()
: a(0),
t([this]() // <- grab 'this' pointer
{
while(true)
{
sleep_for(milliseconds(1000));
a++; // <- use 'a' field of the object (through 'this')
}
})
{
}
};
。