我正在开发Android应用,我已经使用Firebase实现了身份验证。登录后,在我的NavigationDrawerActivity
我有ListView
我已设置FirebaseListAdapater
以获取用户特定数据。
在我的onCreate()
方法中,我有以下内容:
firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser localUser = firebaseAuth.getCurrentUser();
String localUserEmail = localUser.getEmail();
String localUserEmailUniqueID = getEmailAddressUniqueID(localUserEmail);
String dbUsersRoot = "https://DATABASENAME.firebaseio.com/Users/";
String dbUniqueRefForCurrentUser = dbUsersRoot + localUserEmailUniqueID +"/parishioners";
DatabaseReference databaseReferenceCurrentUser = FirebaseDatabase.getInstance().getReferenceFromUrl(dbUniqueRefForCurrentUser);
final FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
this,
String.class,
android.R.layout.simple_list_item_2,
databaseReferenceCurrentUser
) {
@Override
protected void populateView(View v, String model, int position) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
}
};
mListView.setAdapter(firebaseListAdapter);
现在,在我的onStart()
方法中,我向一个按钮添加了一个setOnClickListener
,希望在我的列表中添加一个新项目,如下所示:
@Override
protected void onStart(){
super.onStart();
mButtonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser localUser = firebaseAuth.getCurrentUser();
String localUserEmail = localUser.getEmail();
String localUserEmailUniqueID = getEmailAddressUniqueID(localUserEmail);
String dbUsersRoot = "https://DATABASENAME.firebaseio.com/Users/";
String dbUniqueRefForCurrentUser = dbUsersRoot + localUserEmailUniqueID +"/parishioners";
DatabaseReference databaseReferenceCurrentUser = FirebaseDatabase.getInstance().getReferenceFromUrl(dbUniqueRefForCurrentUser);
databaseReferenceCurrentUser.push().setValue(editTextNewMember.getText().toString().trim());
}
});
}
点击添加按钮后,我在我的数据库中会有这样的内容(假设EditText
中包含字符串&#39; capuccino&#39;):
"luminittta" : {
"parishioners" : {
"-KX1GcEWPMy7oFWgt_3h" : "capuccino"
现在,我的名单将显示&#39; capuccino&#39; (因为它是一个实时数据库,我的列表会自动使用新添加的值进行更新)。
我的问题是:
如何实施下一项删除功能,当用户长时间点击ListView
中的项目时,该功能会被删除?
整个数据库如下所示:
{
"Users" : {
"davidivanmircea" : {
"parishioners" : {
"-KX1FNcgSXgsMNz6QRIS" : "Gus Crick",
"-KX1FVIzzPM4nMrMvm9x" : "Florentino Williams",
"-KX1FZ27M-KS5TQZQ76u" : "Marc Raff",
"-KX1FbercKpPrl9r9tb4" : "Frederick Haddon",
"-KX1FgguOlRRN4sZKS8Z" : "Cristobal Wolfe",
"-KX1FkgwifQLIHplsZNx" : "Scott Nodal",
"-KX1Fp64Sa2QD94GR9uK" : "Odis Nevers",
"-KX1FrYK5PeuBucEiamY" : "Chauncey Mossman",
"-KX1FvIBDSrvppC3e4Ip" : "Alfonso Ignacio",
"-KX1Fy7dEHH8Z9JV-BRL" : "Douglas Hettinger",
"-KX1WdtbV4PEiscJV6E_" : "Daniel Muresan",
"-KX1WvYqekJZdWS6wGh6" : "Angelina Jolie"
}
},
"luminittta" : {
"parishioners" : {
"-KX1GYuSsgmHyGk5LQLA" : "mocha",
"-KX1G_Q130sWitNGzlAR" : "triplo",
"-KX1GaPIhuIyhV4vmLGe" : "latte",
"-KX1GcEWPMy7oFWgt_3h" : "capuccino",
"-KX1Gf7TKY3N8IQETig-" : "machiatto",
"-KX1Gh4KoYpcVpCDFI2x" : "cafe melange",
"-KX1GoCutX3XO-CRWWB3" : "ristretto",
"-KX1GpvdE5i9UR2ZaX0K" : "americano",
"-KX1Grb8S5QZu4HT9cuW" : "espresso"
}
}
}
}
非常感谢你的帮助!
PS:我已经使用&#39; DATABASENAME&#39;修改了我的实际数据库名称。在代码里面。
答案 0 :(得分:1)
将“populateView”更改为:
@Override
protected void populateView(View v, String model, int position) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mRecycleViewAdapter.getRef(position).removeValue();
Log.d("TAG", model + " removed from the list");
}
});
}
};
答案 1 :(得分:1)
要删除LongClick上的特定项目,请在populateView中写入
v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
getRef(position).removeValue();
notifyDataSetChanged();
return true;
}
});
答案 2 :(得分:0)
在阅读Nirel和Frank提供的代码后,我将FirebaseListAdapter成功更改为RecyclerView并实现了我的删除功能(点击列表中的项目)。
对于记录,RecyclerView本身没有onClickListener,因此任何与我相同情况的人都需要实现它。我使用了http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/,它就像一个魅力。