Button在适配器类中不起作用

时间:2016-01-21 18:13:23

标签: php android baseadapter

    public class WishlistAdapter extends BaseAdapter  {
    private ProgressDialog progressDialog;
    private LayoutInflater inflater;
    private Context mContext;
    private List<Response.UserinfoEntity> userinfo;
    private static final String SERVER_ADDRESS = "http://www.bruhnancel.xyz/" ;

    public interface OnItemClickListener {

        public void onItemClicked(int position);

    }
    private OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    public WishlistAdapter(Context mContext, List<Response.UserinfoEntity> userinfo) {
        this.mContext = mContext;
        this.userinfo = userinfo;
    }

    @Override
    public int getCount() {
        return userinfo.size();
    }

    @Override
    public Object getItem(int position) {
        return userinfo.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.wishlist_row_item, parent, false);
        Response.UserinfoEntity item = (Response.UserinfoEntity) getItem(position);
        TextView feedback_name_voted = (TextView)rowView.findViewById(R.id.feedback_name_voted);

       // TextView title = (TextView)rowView.findViewById(R.id.title);

        TextView comment_posted= (TextView)rowView.findViewById(R.id.comment_posted);
        //TextView id = (TextView)rowView.findViewById(R.id.id);
        final TextView like_total_number= (TextView)rowView.findViewById(R.id.like_total_number);
        ImageView pic = (ImageView)rowView.findViewById(R.id.latest_promotion_pic);

        Button delete_wishlist = (Button)rowView.findViewById(R.id.delete_wishlist);
        delete_wishlist.setTag(position);
        feedback_name_voted.setText("Name = " +item.getAd_title());
        comment_posted.setText(item.getCom_name());
        like_total_number.setText(item.getAd_id());
        Picasso.with(mContext)
                .load(item.getImg_link())
                .into(pic);




        delete_wishlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DeleteWishlist(like_total_number.getText().toString()).execute();
                // for the better performance remove auto-boxing
                listener.onItemClicked((Integer)v.getTag());
            }
        });
        return rowView;
    }


    private class DeleteWishlist extends AsyncTask<Void, Void, Void> {


        String  delete;


        public DeleteWishlist( String delete )
        {

            this.delete = delete;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //showProgressDialog("Please wait...", "Your message");

        }

        @Override
        protected Void doInBackground(Void... params) {



            ArrayList<NameValuePair> dataToSend = new ArrayList<>();


            dataToSend.add(new BasicNameValuePair("ad_id", delete));




            try{
                HttpParams httpRequestParams = getHttpRequestParams();
                HttpClient client = new DefaultHttpClient(httpRequestParams);
                HttpPost post = new HttpPost(SERVER_ADDRESS + "delete_wishlst.php");



                post.setEntity(new UrlEncodedFormEntity(dataToSend));
                client.execute(post);

            }catch(Exception e){
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            if(progressDialog != null && progressDialog.isShowing())
            {

                //progressDialog.dismiss();
                Toast.makeText(mContext, "Ads successfully deleted", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(mContext, MainActivity.class);

                mContext.startActivity(intent);
            }
        }
    }

    private HttpParams getHttpRequestParams(){

        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams, 1000 * 30);
        HttpConnectionParams.setSoTimeout(httpRequestParams, 1000*30);
        return httpRequestParams;
    }

    private void showProgressDialog(String title, String message)
    {
        progressDialog = new ProgressDialog(mContext);

        progressDialog.setTitle("Please wait"); //title

        progressDialog.setMessage("Uploading"); // message

        progressDialog.setCancelable(false);

        progressDialog.show();
    }
}

这是我的PHP代码

<?php
    $con=mysqli_connect("localhost","xx","xx","xx");


    $ad_id = $_POST["ad_id"];




    $statement = mysqli_prepare($con, "DELETE  FROM wish_list WHERE ad_id= ?");

        mysqli_stmt_bind_param($statement, "s", $ad_id);

        mysqli_stmt_execute($statement);

        mysqli_stmt_close($statement);

        mysqli_close($con);
?>

错误

01-22 16:23:46.435 18618-18618/com.example.user.mfpv2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.example.user.mfpv2, PID: 18618
                                                                        java.lang.NullPointerException
                                                                            at com.example.user.mfpv2.WishlistAdapter$1.onClick(WishlistAdapter.java:105)
                                                                            at android.view.View.performClick(View.java:4438)
                                                                            at android.view.View$PerformClick.run(View.java:18439)
                                                                            at android.os.Handler.handleCallback(Handler.java:733)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:136)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5034)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:515)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
                                                                            at dalvik.system.NativeStart.main(Native Method)

所以我正在做的是我尝试通过使用php文件发送字符串向我的数据库发送请求。

但它似乎没有用。这是我第一次在适配器类中实现它。

2 个答案:

答案 0 :(得分:0)

首先,尝试使用相同的凭据使用mysql客户端并按原样运行查询,并检查它是否正常工作。

其次,将mysqli_stmt_bind_param($statement, "s", $ad_id);中的“s”更改为“i”,因为ID列应该是整数而不是字符串,除非您的数据库不同。

查看PHP手册了解更多信息http://php.net/manual/en/mysqli-stmt.bind-param.php

如果它仍然无效,请检查是否真的调用了PHP脚本,检查Web服务器的访问日志,以及任何线索的PHP错误日志。

答案 1 :(得分:0)

您需要为最佳情况提供外部接口。

public interface OnItemClickListener {

   public void onItemClicked(int position);

}

并将他设置为适配器。

public class WishlistAdapter extends BaseAdapter  {

// code
private OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}

并在getView方法中使用:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // your code    

        Button delete_wishlist = (Button)rowView.findViewById(R.id.delete_wishlist);
        delete_wishlist.setTag(position);
        // your code
        delete_wishlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // for the better performance remove auto-boxing
                listener.onItemClicked((Integer)v.getTag());   
            }
        });
        return rowView;
    }

更新

您需要设置回调适配器。

// in activity or where you create an adapter

WishlistAdapter adapter = new WishlistAdapter(...);
adapter.setOnItemClickListener(new OnItemClickListener(

    public void onItemClicked(int posisiton) {
        // handle click
    }
));