0

i am using listview with baseadapter . every row listview have 2 button. i set event on thats buttons.

this is my problem before link and this is my code now

 public class customadapter extends BaseAdapter{    

        ArrayList<HashMap<String, String>> oslist;
        Context context;
        private Activity parentActivity;
        private Button btnDelete;
        private Button btnEdit;
        AlertDialog.Builder alertDialogBuilder;
        public String URL = "http://192.168.1.103:8080/exdar/api/registerItem/deleting";

        public customadapter(ArrayList<HashMap<String, String>> oslist,Context context, Activity parentactivity) {  
            System.out.println("skdjfhksdfjskfjhsdkjfh");
            this.context = context;
            this.oslist = oslist;
            this.parentActivity = parentactivity;

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub      
            return oslist.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return oslist.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater lif = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = lif.inflate(R.layout.listitem, null);

        final TextView id = (TextView) convertView.findViewById(R.id.varId);
        TextView noNota = (TextView) convertView.findViewById(R.id.varNoNota);
        TextView senderName = (TextView) convertView.findViewById(R.id.varSenderName);
        TextView totalAmount = (TextView) convertView.findViewById(R.id.varTotalAmount);

        id.setText(oslist.get(position).get("id"));
        noNota.setText(oslist.get(position).get("noNota"));
        senderName.setText(oslist.get(position).get("senderName"));
        totalAmount.setText(oslist.get(position).get("totalAmount"));       

        Button btnEdit = (Button) convertView.findViewById(R.id.btnEdit);
        Button btnDelete = (Button) convertView.findViewById(R.id.btnDelete);

            btnEdit.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    Toast.makeText(context, "Edit ditekan!", Toast.LENGTH_LONG).show();
                    //Here perform the action you want             


                }
            });
            btnDelete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    final Dialog dialog = new Dialog(parentActivity);
                    dialog.setContentView(R.layout.yesnodialog);
                    dialog.setTitle("Delete");
                    TextView text = (TextView) dialog.findViewById(R.id.rusurewanttodelete);                

                    Button YES = (Button) dialog.findViewById(R.id.yes);
                    Button NO = (Button) dialog.findViewById(R.id.no);
                    YES.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                        String response = "";

                        System.out.println(id.getText());   


                        // I  ADD THIS CODE  FROM HTTPUTILS NETWORKGET 

                        HttpUtils networkGet = HttpUtils.getInstance();
                        ArrayList<NameValuePair> parameter = new ArrayList<NameValuePair>();
                        parameter.add(new BasicNameValuePair("id", (String) id.getText()));                     
                        try {
                            System.out.println("aaaaaaaaaaaaaaaa");
                            System.out.println("URLURLURLURL = "+URL);
                            response = networkGet.postToHTTP(URL, parameter);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                       // UNTIL THIS


                            oslist.remove(position);
                            notifyDataSetChanged();
                            dialog.dismiss();
                            Toast.makeText(context, "Data Deleted.", Toast.LENGTH_LONG).show();
                        }
                    });

                    NO.setOnClickListener(new OnClickListener() {                   
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                        }
                    });

                    dialog.show();
                }
            });
        return convertView;
        }   
    }

and i get this error

enter image description here

anyone know why i get this error, and solusion?

Community
  • 1
  • 1

2 Answers2

0

Yo are making http request on main thread, you can't do this in Android. Use AsyncTask or Threadinstead. http://developer.android.com/guide/components/processes-and-threads.html

Tofasio
  • 425
  • 3
  • 14
0

Check this out

 HttpUtils networkGet = HttpUtils.getInstance();
                        ArrayList<NameValuePair> parameter = new ArrayList<NameValuePair>();
                        parameter.add(new BasicNameValuePair("id", (String) id.getText()));                     
                        try {
                            System.out.println("aaaaaaaaaaaaaaaa");
                            System.out.println("URLURLURLURL = "+URL);
                            response = networkGet.postToHTTP(URL, parameter);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

here this line

 response = networkGet.postToHTTP(URL, parameter);

is asking to access internet and it is on main thread. so put all the networking in asyntask

Jamil
  • 5,457
  • 4
  • 26
  • 29