gpt4 book ai didi

php - 如何使用 volley 删除 recyclerview 中的项目?

转载 作者:行者123 更新时间:2023-11-29 05:56:28 25 4
gpt4 key购买 nike

您好,我已通过将回收 View 与 MYSQL PHP 链接成功地插入项目,但删除项目仍然失败。你可以在客户端删除它,但我用 PHP 和 MYSQL 总是失败。我可以获得有关此示例或代码的帮助吗?

这是recyclerview适配器代码

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

//Imageloader to load image
private ImageLoader imageLoader;
private Context context;

//List to store all superheroes
List<recyclerview_list> recyclerview_lists;

//Constructor of this class
public CardAdapter(List<recyclerview_list> recyclerview_lists, recyclerview context) {
super();
//Getting all superheroes
this.recyclerview_lists = recyclerview_lists;
this.context = (Context) context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_list, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

//Getting the particular item from the list
recyclerview_list recyclerview_List = recyclerview_lists.get(position);

//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(recyclerview_List.getImage_path(), ImageLoader.getImageListener(holder.imageView, R.drawable.ic_launcher_background, android.R.drawable.ic_dialog_alert));

//Showing data on the views
holder.imageView.setImageUrl(recyclerview_List.getImage_path(), imageLoader);
holder.textViewName.setText(recyclerview_List.getImage_name());

}

@Override
public int getItemCount() {
return recyclerview_lists.size();
}

class ViewHolder extends RecyclerView.ViewHolder {
//Views
public NetworkImageView imageView;
public TextView textViewName;
public Button deleteButton;

//Initializing Views
public ViewHolder(final View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
deleteButton = (Button) itemView.findViewById(R.id.buttonDelete);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Delete", Toast.LENGTH_SHORT).show();
delete(getAdapterPosition());


}
});

}

public void delete(int position) {

try {
recyclerview_lists.remove(position);
notifyItemRemoved(position);
} catch (IndexOutOfBoundsException ex) {
ex.printStackTrace();
}
}
}
}

这是PHP代码

$con = mysqli_connect("", "", "", "");

$id = $_POST["id"];

$statement = mysqli_prepare($con, "DELETE FROM image WHERE id = ?");
mysqli_stmt_bind_param($statement, "s", $id);
mysqli_stmt_execute($statement);

$response = array();
$response["success"] = true;

echo json_encode($response);

最佳答案

在适配器调用中添加新方法

  public void deleteFromServer(int id)
{
url = "http://yoururl/delete_image.php";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// if success
delete(id)
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", response);
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("id", id);

return params;
}
};
queue.add(postRequest);
}

从适配器类调用这个方法

deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Delete", Toast.LENGTH_SHORT).show();
deleteFromServer(recyclerview_lists.get(getAdapterPosition()).id);


}
});

关于php - 如何使用 volley 删除 recyclerview 中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49021547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com