实际上,这是我第一次使用RecyclerView,我尝试从列表中删除项目以用户身份查看menuItem中的删除按钮.... 但是在单击“删除”选项后,我的项目在我的数据库上是删除但我的适配器未更新... 帮助我如何设置我的适配器
我的代码:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
public static final String DELETE_URL = "http://delete.php";
String KEY_UNIQUE_ID="id";
Context context;
List<GetDataAdapter> getDataAdapter;
ImageLoader imageLoader1;
public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder Viewholder, final int position) {
final GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();
imageLoader1.get(getDataAdapter1.getImageServerUrl(),
ImageLoader.getImageListener(
Viewholder.CircularNetworkImageView,//Server Image
R.drawable.loading,//Before loading server image the default showing image.
R.drawable.loading //Error image if requested image dose not found on server.
)
);
Viewholder.CircularNetworkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);
Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());
Viewholder.UserAge.setText(getDataAdapter1.getImageAge());
Viewholder.User_Gender.setText(getDataAdapter1.getImageGender());
Viewholder.overflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupMenu(Viewholder.overflow,getDataAdapter1.getId());
}
});
}
/**
* Showing popup menu when tapping on 3 dots
*/
private void showPopupMenu(View view, String position) {
// inflate menu
PopupMenu popup = new PopupMenu(context, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_album, popup.getMenu());
popup.setOnMenuItemClickListener(new MyMenuItemClickListener(position));
// Toast.makeText(context, position, Toast.LENGTH_SHORT).show();
popup.show();
}
/**
* Click listener for popup menu items
*/
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
private String position;
public MyMenuItemClickListener(String position) {
this.position=position;
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.Delete:
Toast.makeText(context, "Delete option"+position, Toast.LENGTH_SHORT).show();
delete(position);
return true;
case R.id.Edit_Option:
Toast.makeText(context, "Edit option", Toast.LENGTH_SHORT).show();
return true;
default:
}
return false;
}
}
private void delete(String id){
//http call for Delete the item and it's successfully work but my list is not updated
}
@Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView ImageTitleNameView,UserAge,User_Gender;
public ImageView overflow;
public Parse.CircularNetworkImageView CircularNetworkImageView ;
public ViewHolder(View itemView) {
super(itemView);
ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item);
UserAge=(TextView) itemView.findViewById(R.id.age);
User_Gender =(TextView) itemView.findViewById(R.id.gender);
overflow = (ImageView) itemView.findViewById(R.id.overflow);
CircularNetworkImageView = (CircularNetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1);
}
}
答案 0 :(得分:2)
首先改变这个:
Viewholder.overflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupMenu(Viewholder.overflow,position);
}
});
然后改变这个方法:
private void showPopupMenu(View view, int position) {
// inflate menu
PopupMenu popup = new PopupMenu(context, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_album, popup.getMenu());
popup.setOnMenuItemClickListener(new MyMenuItemClickListener(position));
// Toast.makeText(context, position, Toast.LENGTH_SHORT).show();
popup.show();
}
现在,在menuitemClickListener方法内部更改:
private int position;
public MyMenuItemClickListener(int position) {
this.position=position;
}
最后,chnage这个方法:
private void delete(int id){
//http call for Delete the item and it's successfully work but my list is not updated
getDataAdapter.remove(id);
notifyItemRemoved(id);
}
答案 1 :(得分:1)
只需致电$insert_rows = $db->prepare("INSERT INTO data_sink (data1, data2, data3)
SELECT username, usergroup, ? FROM data_origins WHERE <condition>");
$insert_rows->bindParam(1, $data3);
$insert_rows->execute();
并从arraylist或列表中删除该项目!