Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用截击删除recyclerview中的项目?_Php_Android_Mysql_Android Recyclerview - Fatal编程技术网

Php 如何使用截击删除recyclerview中的项目?

Php 如何使用截击删除recyclerview中的项目?,php,android,mysql,android-recyclerview,Php,Android,Mysql,Android Recyclerview,嗨,我已经通过链接回收视图和MYSQL PHP成功地插入了项目,但是删除项目仍然失败。您可以在客户端删除它,但我在PHP和MYSQL方面一直失败。我可以得到这个例子或代码的帮助吗 这是recyclerview适配器代码 public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> { //Imageloader to load image private ImageLoad

嗨,我已经通过链接回收视图和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();
            }
        }
    }
}

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

  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);
        }

很好的代码,但无法获取识别的id。我将分享我的全部工作

Config.java

public static final String DATA_URL = "http:// /images/feed.php?page=";

/*Json으로 가져오는 이미지 테이블의 목록*/

// 이미지 주소
public static final String TAG_IMAGE_URL = "image_path";
// 이미지 이름
public static final String TAG_NAME = "image_name";
}

自定义截击请求.java

private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;

private CustomVolleyRequest(Context context) {

    this.context = context;
    this.requestQueue = getRequestQueue();

    imageLoader = new ImageLoader(requestQueue,
            new ImageLoader.ImageCache() {
                private final LruCache<String, Bitmap>
                        cache = new LruCache<String, Bitmap>(20);

                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }

                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            });
}

public static synchronized CustomVolleyRequest getInstance(Context context) {
    if (customVolleyRequest == null) {
        customVolleyRequest = new CustomVolleyRequest(context);
    }
    return customVolleyRequest;
}

public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
        Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
        Network network = new BasicNetwork(new HurlStack());
        requestQueue = new RequestQueue(cache, network);
        requestQueue.start();
    }
    return requestQueue;
}

public ImageLoader getImageLoader() {
    return imageLoader;
}
回收视图。爪哇

//创建超级英雄列表 私人名单超级英雄

//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;


//Volley Request Queue
private RequestQueue requestQueue;

//The request counter to send ?page=1, ?page=2  requests
private int requestCount = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recyclerview);

    //Initializing Views
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    //Initializing our superheroes list
    listSuperHeroes = new ArrayList<>();
    requestQueue = Volley.newRequestQueue(this);

    //Calling method to get data to fetch data
    getData();

    //Adding an scroll change listener to recyclerview
    recyclerView.setOnScrollChangeListener(this);

    //initializing our adapter
    adapter = new CardAdapter(listSuperHeroes ,this);

    //Adding adapter to recyclerview
    recyclerView.setAdapter(adapter);
}

//Request to get json from server we are passing an integer here
//This integer will used to specify the page number for the request ?page = requestcount
//This method would return a JsonArrayRequest that will be added to the request queue
private JsonArrayRequest getDataFromServer(int requestCount) {
    //Initializing ProgressBar
    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    //Displaying Progressbar
    progressBar.setVisibility(View.VISIBLE);
    setProgressBarIndeterminateVisibility(true);

    //JsonArrayRequest of volley
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL + String.valueOf(requestCount),
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    //Calling method parseData to parse the json response
                    parseData(response);
                    //Hiding the progressbar
                    progressBar.setVisibility(View.GONE);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressBar.setVisibility(View.GONE);
                    //If an error occurs that means end of the list has reached
                    Toast.makeText(recyclerview.this, "No More Items Available", Toast.LENGTH_SHORT).show();
                }
            });

    //Returning the request
    return jsonArrayRequest;
}

//This method will get data from the web api
private void getData() {
    //Adding the method to the queue by calling the method getDataFromServer
    requestQueue.add(getDataFromServer(requestCount));
    //Incrementing the request counter
    requestCount++;
}

//This method will parse json data
private void parseData(JSONArray array) {
    for (int i = 0; i < array.length(); i++) {
        //Creating the superhero object
        recyclerview_list recyclerviewList = new recyclerview_list();
        JSONObject json = null;
        try {
            //Getting json
            json = array.getJSONObject(i);

            //Adding data to the superhero object
            recyclerviewList.setImage_path(json.getString(Config.TAG_IMAGE_URL));
            recyclerviewList.setImage_name(json.getString(Config.TAG_NAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //Adding the superhero object to the list
        listSuperHeroes.add(recyclerviewList);
    }

    //Notifying the adapter that data has been added or changed
    adapter.notifyDataSetChanged();
}

//This method would check that the recyclerview scroll has reached the bottom or not
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
    if (recyclerView.getAdapter().getItemCount() != 0) {
        int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
        if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
            return true;
    }
    return false;
}

//Overriden method to detect scrolling
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
    //Ifscrolled at last then
    if (isLastItemDisplaying(recyclerView)) {
        //Calling the method getdata again
        getData();
    }
}
//创建视图
私人回收站;
private RecyclerView.LayoutManager LayoutManager;
专用循环视图适配器;
//截击请求队列
私有请求队列请求队列;
//发送请求的请求计数器?page=1,?page=2请求
private int requestCount=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u recyclerview);
//初始化视图
recyclerView=(recyclerView)findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager=新的LinearLayoutManager(此);
recyclerView.setLayoutManager(layoutManager);
//正在初始化我们的超级英雄列表
ListSuperheros=newArrayList();
requestQueue=Volley.newRequestQueue(this);
//调用方法获取数据以获取数据
getData();
//向recyclerview添加滚动更改侦听器
recyclerView.setOnScrollChangeListener(此);
//初始化适配器
适配器=新的CardAdapter(列表超级英雄,此);
//向recyclerview添加适配器
recyclerView.setAdapter(适配器);
}
//请求从服务器获取json我们在这里传递一个整数
//此整数将用于指定请求的页码?page=requestcount
//此方法将返回将添加到请求队列的JsonArrayRequest
私有JsonArrayRequest getDataFromServer(int requestCount){
//初始化进度条
最终ProgressBar ProgressBar=(ProgressBar)findViewById(R.id.progressBar1);
//显示进度条
progressBar.setVisibility(View.VISIBLE);
SetProgressBarInDeterminateVibility(真);
//JsonArrayRequest截击
JsonArrayRequest JsonArrayRequest=新的JsonArrayRequest(Config.DATA\u URL+String.valueOf(requestCount),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
//调用parseData方法来解析json响应
解析数据(响应);
//隐藏进度条
progressBar.setVisibility(View.GONE);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
progressBar.setVisibility(View.GONE);
//若发生错误,则表示已到达列表末尾
Toast.makeText(recyclerview.this,“没有更多可用项”,Toast.LENGTH_SHORT.show();
}
});
//返回请求
返回jsonArrayRequest;
}
//此方法将从web api获取数据
私有void getData(){
//通过调用getDataFromServer方法将该方法添加到队列
add(getDataFromServer(requestCount));
//递增请求计数器
requestCount++;
}
//此方法将解析json数据
私有void parseData(JSONArray数组){
对于(int i=0;i
您没有调用任何Web服务来从服务器中删除数据。按delete按钮时,您必须调用PHP文件。在当前代码中,它只从本地列表中删除项,所以我不知道如何在适配器类中调用PHP文件。。有什么例子吗?
private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;

private CustomVolleyRequest(Context context) {

    this.context = context;
    this.requestQueue = getRequestQueue();

    imageLoader = new ImageLoader(requestQueue,
            new ImageLoader.ImageCache() {
                private final LruCache<String, Bitmap>
                        cache = new LruCache<String, Bitmap>(20);

                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }

                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            });
}

public static synchronized CustomVolleyRequest getInstance(Context context) {
    if (customVolleyRequest == null) {
        customVolleyRequest = new CustomVolleyRequest(context);
    }
    return customVolleyRequest;
}

public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
        Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
        Network network = new BasicNetwork(new HurlStack());
        requestQueue = new RequestQueue(cache, network);
        requestQueue.start();
    }
    return requestQueue;
}

public ImageLoader getImageLoader() {
    return imageLoader;
}
private  String image_path;
private  String image_name;

public String getImage_path() {
    return image_path;
}

public void setImage_path(String image_path) {
    this.image_path = image_path;
}

public String getImage_name() {
    return image_name;
}

public void setImage_name(String image_name) {
    this.image_name = image_name;
}
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;


//Volley Request Queue
private RequestQueue requestQueue;

//The request counter to send ?page=1, ?page=2  requests
private int requestCount = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recyclerview);

    //Initializing Views
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    //Initializing our superheroes list
    listSuperHeroes = new ArrayList<>();
    requestQueue = Volley.newRequestQueue(this);

    //Calling method to get data to fetch data
    getData();

    //Adding an scroll change listener to recyclerview
    recyclerView.setOnScrollChangeListener(this);

    //initializing our adapter
    adapter = new CardAdapter(listSuperHeroes ,this);

    //Adding adapter to recyclerview
    recyclerView.setAdapter(adapter);
}

//Request to get json from server we are passing an integer here
//This integer will used to specify the page number for the request ?page = requestcount
//This method would return a JsonArrayRequest that will be added to the request queue
private JsonArrayRequest getDataFromServer(int requestCount) {
    //Initializing ProgressBar
    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    //Displaying Progressbar
    progressBar.setVisibility(View.VISIBLE);
    setProgressBarIndeterminateVisibility(true);

    //JsonArrayRequest of volley
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL + String.valueOf(requestCount),
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    //Calling method parseData to parse the json response
                    parseData(response);
                    //Hiding the progressbar
                    progressBar.setVisibility(View.GONE);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressBar.setVisibility(View.GONE);
                    //If an error occurs that means end of the list has reached
                    Toast.makeText(recyclerview.this, "No More Items Available", Toast.LENGTH_SHORT).show();
                }
            });

    //Returning the request
    return jsonArrayRequest;
}

//This method will get data from the web api
private void getData() {
    //Adding the method to the queue by calling the method getDataFromServer
    requestQueue.add(getDataFromServer(requestCount));
    //Incrementing the request counter
    requestCount++;
}

//This method will parse json data
private void parseData(JSONArray array) {
    for (int i = 0; i < array.length(); i++) {
        //Creating the superhero object
        recyclerview_list recyclerviewList = new recyclerview_list();
        JSONObject json = null;
        try {
            //Getting json
            json = array.getJSONObject(i);

            //Adding data to the superhero object
            recyclerviewList.setImage_path(json.getString(Config.TAG_IMAGE_URL));
            recyclerviewList.setImage_name(json.getString(Config.TAG_NAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //Adding the superhero object to the list
        listSuperHeroes.add(recyclerviewList);
    }

    //Notifying the adapter that data has been added or changed
    adapter.notifyDataSetChanged();
}

//This method would check that the recyclerview scroll has reached the bottom or not
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
    if (recyclerView.getAdapter().getItemCount() != 0) {
        int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
        if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
            return true;
    }
    return false;
}

//Overriden method to detect scrolling
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
    //Ifscrolled at last then
    if (isLastItemDisplaying(recyclerView)) {
        //Calling the method getdata again
        getData();
    }
}