试图进行大量搜索,但是两天后,我仍然对这个问题感到困惑。
我的Firebase中有以下数据库,该数据库填充在我的 def create
calendar_id = params[:calendar][:id]
start_date = params[:calendar][:start_date]
start_time = params[:calendar][:start_time]
time_zone = params[:calendar][:time_zone]
lesson_hash = get_module_lectures(module_number)
# Time.zone = 'America/New_York'
lesson_hash.each do |days_from_start, lesson_name|
lesson_datetime = Time.parse("#{start_date} #{start_time}").advance(:days => days_from_start)
new_event(calendar_id, lesson_datetime, lesson_name, time_zone)
end
redirect_to calendars_url
end
def new_event(calendar_id, lesson_datetime, lesson_name, time_zone)
client = Signet::OAuth2::Client.new(client_options)
client.update!(session[:authorization])
service = Google::Apis::CalendarV3::CalendarService.new
service.authorization = client
event = Google::Apis::CalendarV3::Event.new({
# start: Google::Apis::CalendarV3::EventDateTime.new(datetime: lesson_datetime),
# end: Google::Apis::CalendarV3::EventDateTime.new(datetime: lesson_datetime.advance(:hours => 1)),
start: {
'date_time': lesson_datetime,
# 'time_zone': 'America/New_York'
},
end:{
'date_time': lesson_datetime.advance(:hours => 1),
# 'time_zone': 'America/New_York'
},
summary: lesson_name
})
service.insert_event(calendar_id, event)
end
中。
对于ListView
中的每一行,都会显示data1和data2。
ListView
直到现在一切顺利,我可以从应用程序上传数据,并且列表视图与Firebase控制台完全同步。
我使用以下代码制作了ListView:
test-3db7e
|
+---users
|
+---W9KkXAidmHgyOpyeQPeT5YxgQI42
|
+---data
| |
| +----LIv-OeQdixT0q6NZ-jL
| | |
| | +---data1: "Data1"
| | |
| | +---data2: "Data2"
| |
| +----LIv-R3PRKaEHaAcMjWu
| |
| +---data1: "Data1"
| |
| +---data2: "Data2"
|
+---name: "The user's name"
我还创建了Data.class来将先前代码中的值存储到列表视图中。
//Updating the listview
databaseReference.child("users").child(user).child("data").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//Getting all the children at this level
Iterable<DataSnapshot> children = dataSnapshot.getChildren();
dataArrayAdapter.clear();
for (DataSnapshot child : children) {
Data data = child.getValue(Data.class);
dataList.add(data);
dataArrayAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
现在的问题是以下代码,长按要删除的行(如果在对话框中按是),长按要删除的行。
package com.mypackge.firebase;
public class Data {
private String data1;
private String data2;
public String getData1() {
return data1;
}
public void setData1(String data1) {
this.data1 = data1;
}
public String getData2() {
return data2;
}
public void setData2(String data2) {
this.data2 = data2;
}
public String toString() {
return data1 +"\n"+data2;
}
}
谢谢大家的宝贵时间!
答案 0 :(得分:0)
解决此问题的关键是将创建pushed key
类的对象时生成的Data
添加到数据库中。因此,在data1
和data2
字段旁边,添加另一个名为key
(也是String类型)的字段。请同时为该新字段添加相应的setter和getter。
现在,这是实际上需要在对象上创建的方式:
DatabaseReference dataRef = databaseReference.child("users").child(user).child("data");
String key = dataRef.push().getKey();
Data data = new Data();
data.setData1("data1");
data.setData2("data2");
data.setKey(key);
dataRef.child(key).setValue(data);
现在,您的Data
对象的key
字段中填充了数据库中的实际密钥。要显示数据并删除单击的特定对象,请使用以下代码:
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Data> list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Data data = ds.getValue(Data.class);
list.add(data);
}
ListView listView = (ListView) findViewById(R.id.list_view);
ArrayAdapter<Data> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage("Do you want to delete?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
String key = arrayAdapter.getItem(i).getKey();
dataRef.child(key).removeValue();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
};
dataRef.addListenerForSingleValueEvent(valueEventListener);