我的列表视图项包含一个删除按钮,我在我的适配器中将其设置为从数据库中删除该项。现在,一旦完成,我如何刷新包含listView的片段以删除该项目?相关代码如下:
删除适配器中的项目功能:
holder.actionIndicator.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
alertDialog.setTitle("Delete item");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
FirebaseMethods firebaseMethods = new FirebaseMethods(mContext);
firebaseMethods.deleteHashtagFromDatabase(title);
dialog.dismiss();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "no",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
});
Listview在我的片段中设置如下:
private void setupListView(){
Log.d(TAG, "setupListView: setting up list");
final ArrayList<HashTag> hashTags = new ArrayList<>();
myRef = FirebaseDatabase.getInstance().getReference();
Query query = myRef.child(getString(R.string.dbname_user_hashtags))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
if(singleSnapshot != null){
hashTags.add(0, singleSnapshot.getValue(HashTag.class));
}
}
HashTagListViewAdapter adapter = new HashTagListViewAdapter(getActivity(), R.layout.layout_list_itemview, hashTags);
hashtagListView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
谢谢!
更新
现在我已将subItem上的OnClickListener从适配器传递到片段ListView OnItemClickListener,如下所示:
更新了适配器
holder.actionIndicator.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
更新了片段
hashtagListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
long viewId = view.getId();
Log.d(TAG, "onItemClick: viewId is " + viewId);
Log.d(TAG, "onItemClick: position is " + position);
final HashTagListViewAdapter adapter = new HashTagListViewAdapter(getActivity(), R.layout.layout_list_itemview, hashTags);
final HashTag hashTag = hashTags.get(position);
final String title = hashTag.getTitle();
Log.d(TAG, "onItemClick: title is " + title);
if(viewId == R.id.deleteFromListBtn){
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle("delete item");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
FirebaseMethods firebaseMethods = new FirebaseMethods(getActivity());
firebaseMethods.deleteHashtagFromDatabase(title);
Log.d(TAG, "onClick: title is " + title);
// NEW PROBLEM HERE!
adapter.remove(hashTag);
Log.d(TAG, "onClick: removed hashtag is " + hashTag);
adapter.notifyDataSetChanged();
Log.d(TAG, "onItemClick: item removed from list.");
dialog.dismiss();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "no",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}
});
但现在问题是当我删除项目时,listView总是删除视图中的最后一项。但是,正确的项目已从数据库中删除,并且根据日志,此代码adapter.remove(hashTag)
会抓取我打算删除的正确项目。
答案 0 :(得分:1)
首先,你不应该刷新整个Fragment
。坏习惯
刷新ListView
,
ArrayList
中删除该项目(名为&#39; hashTags&#39;)。YOUR_ADAPTER.notifyDataSetChanged()
。 notifyDataSetChanged()会自动从ListView
删除项目。
答案 1 :(得分:1)
首先应该从数据库中删除该项,当成功来自数据库时,应该从ArrayList中删除该项,并且适配器应该通知ArratList中所做的更改。
从ArrayList表单适配器类中删除项目。
//postion is the location of item in the arraylist to be deleted.
dataSet.remove(position);
现在告知适配器有关ArrayList中的更改。应该调用Adapter类。
notifyDataSetChanged();