jeudi 19 avril 2018

How to send multiple selected check box value on server in android my Code is:

Hello Friends i want send the multiple selected check box travel id value to server its code working but the value not update together its update one by one when click button, i want when i am click on submit button all selected check box value send on server and the selected check box value update on server .

package com.example.fiffa.fifaa;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ViewSubmission extends AppCompatActivity{
    final Context context = this;
    ListView listView;
    String travelid;
    TextView txtvisit,txtsum,txtsystem;
    Button submit,back;
    String MyPREFERENCES = "loginInfo";
    SharedPreferences sharedpreferences;
    ProgressDialog pDialog;
    String ss="";
    String userid;
    boolean[] present;
    boolean[] absent;
    String takenatt="";
    double gtotal=0.0;
    List<Submitconvencelistitem> rowItems;
    ArrayList<Submitconvencelistitem> feedsList;
    Submitconvenceadapter adapter;
    int count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_submission);
        submit = (Button) findViewById(R.id.submit);
        back = (Button) findViewById(R.id.backviewsub);
        txtvisit=(TextView)findViewById(R.id.txtvisit);
        txtsum=(TextView)findViewById(R.id.txttotal);
        //txtsystem=(TextView)findViewById(R.id.txtsystem);
        listView = (ListView) findViewById(R.id.lstconveynce);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent viewsub=new Intent(ViewSubmission.this,Submitconveyance.class);
                startActivity(viewsub);
            }
        });
        String viewdata = getIntent().getExtras().getString("response", "");
        Log.e("viewdata", viewdata);
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        //conveyncetravelid = sharedpreferences.getString("travelid", "");
        //Log.e("econveyncetravelid", conveyncetravelid);
        userid = sharedpreferences.getString("userId", "");
        Log.e("userId", userid);

         try {

                //feedsList=getSubmit(false);
                JSONArray js = new JSONArray(viewdata);
                feedsList=new ArrayList<>();
                for (int i = 0; i < js.length(); i++) {
                    JSONObject jObject = js.getJSONObject(i);
                    Submitconvencelistitem st=new Submitconvencelistitem();
                    String total_amount=jObject.getString("total_amount");
                    Log.e("total_amount",total_amount);
                    //String travelid = jObject.getString("travelid");
                    //Log.e("conveyncetravelid", travelid);
                    st.settravelid(jObject.optString("travelid"));
                    st.setsubdate(jObject.optString("startdate"));
                    st.setstartlocation(jObject.optString("startlocation"));
                    st.setendlocation(jObject.optString("endlocation"));
                    st.setccno(jObject.optString("cc_no"));
                    st.setcustomername(jObject.optString("custname"));
                    st.setcallstatus(jObject.optString("callstatus"));;
                    st.setmodeofconveynce(jObject.optString("travel_mode"));
                    st.setkmstravelled(jObject.optString("KM"));
                    st.setclaimamout(jObject.optString("total_amount"));
                    //st.setSelected(true);
                    feedsList.add(st);
                /*    gtotal += Double.parseDouble(feedsList.get(i).getclaimamout());
                    Log.e("Total", String.valueOf(gtotal));
                    txtsum.setText(Double.toString(gtotal));*/

                    //Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH_LONG).show();
               /* Log.e("shams", viewdata.toString());
                String startdate = js.getJSONObject(0).getString("startdate");
                Log.e("startdate",startdate);
                String startlocation = js.getJSONObject(0).getString("startlocation");
                Log.e("startlocation",startlocation);
                String endlocation = js.getJSONObject(0).getString("endlocation");
                Log.e("endlocation",endlocation);
                String cc_no = js.getJSONObject(0).getString("cc_no");
                Log.e("cc_no",cc_no);
                String custname = js.getJSONObject(0).getString("custname");
                Log.e("custname",custname);
                String callstatus = js.getJSONObject(0).getString("callstatus");
                Log.e("callstatus",callstatus);
                String travel_mode = js.getJSONObject(0).getString("travel_mode");
                Log.e("travel_mode",travel_mode);
                String KM = js.getJSONObject(0).getString("KM");
                Log.e("KM",KM);
                String total_amount = js.getJSONObject(0).getString("total_amount");
                Log.e("total_amount",total_amount);
                st.setsubdate(startdate);
                st.setstartlocation(startlocation);
                st.setendlocation(endlocation);
                st.setccno(cc_no);
                st.setcustomername(custname);
                st.setcallstatus(callstatus);
                st.setmodeofconveynce(travel_mode);
                st.setkmstravelled(KM);
                st.setclaimamout(total_amount);
                st.setSelected(true);*//*
                feedsList.add(st);*/
                /* JSONObject post = js.getJSONObject(i);
                Submitconvencelistitem st=new Submitconvencelistitem();
                st.setsubdate(post.optString("startdate"));
                st.setstartlocation(post.optString("startlocation"));
                st.setendlocation(post.optString("endlocation"));
                st.setccno(post.optString("cc_no"));
                st.setcustomername(post.optString("custname"));
                st.setcallstatus(post.optString("callstatus"));
                st.setmodeofconveynce(post.optString("travel_mode"));
                st.setkmstravelled(post.optString("KM"));
                st.setclaimamout(post.optString(("total_amount")));
                st.setSelected(true);
                feedsList.add(st);*/
                }
                adapter = new Submitconvenceadapter(ViewSubmission.this, feedsList);
                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                count=adapter.getCount();
                txtvisit.setText(Integer.toString(count));


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


        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //final List<Submitconvencelistitem> selectedItems = adapter.getSelectedItems();
                //Use this data for sending to your webserver
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ViewSubmission.this);
                alertDialogBuilder.setMessage("Submit the Selected Conveynce");
                alertDialogBuilder.setPositiveButton("yes",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                for (int strt=0;strt<present.length;strt++) {
                                    if (present[strt]) {
                                        travelid = feedsList.get(strt).gettravelid();

                                    }
                                    new Asyncupdate_travelforconv().execute();

                                }


                                //Toast.makeText(ViewSubmission.this,"Your conveyance has been submitted ",Toast.LENGTH_LONG).show();

                            }
                        });
                alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       Intent view=new Intent(ViewSubmission.this,Submitconveyance.class);
                       startActivity(view);
                    }
                });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
                //new Asncselect_travelforconvsub().execute();
            }
        });
    }

 /*  private ArrayList<Submitconvencelistitem> getSubmit(boolean isSelect){
        ArrayList<Submitconvencelistitem> list = new ArrayList<>();
        for(int i = 0; i < feedsList.size(); i++){
            Submitconvencelistitem st = new Submitconvencelistitem();
            st.setSelected(isSelect);
            feedsList.add(st);
        }
        return list;
    }
*/
 /*Asynctask For Insertstart travel details*/
        private class Asyncupdate_travelforconv extends AsyncTask<String, Integer, String> {
            String SOAP_ACTION = "http://tempuri.org/IFIFA/update_travelforconv";
            String NAMESPACE = "http://tempuri.org/";
            String METHOD_NAME = "update_travelforconv";
            String URL = "http://migadget.aforeserve.co.in/FIFA.svc?singleWsdl";

            @Override
            protected void onPreExecute() {

            }

            @Override
            protected String doInBackground(String... strings) {
                try {
                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    request.addProperty("travelid", travelid);
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    //envelope.setOutputSoapObject(request);

                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                    try {
                        androidHttpTransport.call(SOAP_ACTION, envelope);

                        Object result = null;
                        result = (Object) envelope.getResponse();
                        Log.e("conveynce", String.valueOf(result));

                        System.out.println("APIresult:" + result.toString());

                        ss = result.toString();
                        Log.e("APIRESULT", ss);
                    } catch (Exception e) {
                        System.out.println("Error" + e);
                    }
                    return ss;
                } catch (Exception ex) {
                    ex.printStackTrace();
                    return "";
                }
            }

            @Override
            protected void onPostExecute(String result) {
                Log.e("APIResult", result);
                    Toast.makeText(getBaseContext(), "Data Updated", Toast.LENGTH_LONG).show();


            }
        }


/*Adapter class For Submit conveynce   */
 public class Submitconvenceadapter extends BaseAdapter {
    ArrayList<Submitconvencelistitem> listData;
        private LayoutInflater layoutInflater;
        Context context;
        List<Submitconvencelistitem> rowItems;
        String MyPREFERENCES = "loginInfo";
        String travelid;
        SharedPreferences sharedpreferences;


        public Submitconvenceadapter(Context ViewSubmission,ArrayList<Submitconvencelistitem> results)
        {
            listData = results;
            context=ViewSubmission;
            present=new boolean[listData.size()];
            Arrays.fill(present, true);
        }
        @Override
        public int getCount()
        {
            return listData.size();
        }

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

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            final ViewHolder holder;
            LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.list_item_submitconveynce, null);
                holder = new ViewHolder();
                //holder.checkbox=(CheckBox) convertView.findViewById(R.id.cb1);
                holder.date = (TextView) convertView.findViewById(R.id.subdate);
                holder.startlocation = (TextView) convertView.findViewById(R.id.startlocation);
                holder.endlocation=(TextView)convertView.findViewById(R.id.endlocation);
                holder.ccno = (TextView) convertView.findViewById(R.id.ccno);
                holder.customername = (TextView) convertView.findViewById(R.id.tv_name);
                holder.callstatus = (TextView)convertView.findViewById(R.id.tv_callstatus);
                holder.modeofconveynce = (TextView)convertView.findViewById(R.id.tv_modconveynce);
                holder.Kmtravelled = (TextView)convertView.findViewById(R.id.tv_km);
                holder.claimamount = (TextView)convertView.findViewById(R.id.claim_amount);
                holder.checkbox=(CheckBox)convertView.findViewById(R.id.chk1);

                /*//holder.checkbox.setChecked(true);
                holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        listData.get(position).setSelected(isChecked);
                    }
                });*/
                convertView.setTag(holder);
                holder.checkbox.setChecked(present[position]);

            }

            else
            {
                holder = (ViewHolder) convertView.getTag();
                holder.checkbox.setChecked(present[position]);
            }
            holder.checkbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (holder.checkbox.isChecked())
                        present[position] = true;
                    else
                        present[position] = false;
                }
            });

            //travelid=listData.get(position).gettravelid();

            //holder.travelid.setText(listData.get(position).gettravelid());
            holder.date.setText(listData.get(position).getsubdate());
            holder.startlocation.setText(listData.get(position).getstartlocation());
            holder.endlocation.setText(listData.get(position).getendlocation());
            holder.ccno.setText(listData.get(position).getccno());
            holder.customername.setText(listData.get(position).getcustomername());
            holder.callstatus.setText(listData.get(position).getcallstatus());
            holder.modeofconveynce.setText(listData.get(position).getmodeofconveynce());
            holder.Kmtravelled.setText(listData.get(position).getkmstravelled());
            holder.claimamount.setText(listData.get(position).getclaimamout());
            //holder.checkbox.setChecked(true);

            return  convertView;

        }



        public class ViewHolder {
            CheckBox checkbox;
            TextView date;
            TextView startlocation;
            TextView endlocation;
            TextView ccno;
            TextView customername;
            TextView callstatus;
            TextView modeofconveynce;
            TextView Kmtravelled;
            TextView claimamount;
            TextView travelid;


        }

     /*   public List<Submitconvencelistitem> getSelectedItems()
        {
            List<Submitconvencelistitem> selectedItems = new ArrayList<>();
            for(Submitconvencelistitem item : listData)
            {
                selectedItems.add(item);
            }
            return selectedItems;
        }*/
    }

}




Aucun commentaire:

Enregistrer un commentaire