0

I am working on a project that has a custom listview containing dynamic data. What I want is to fetch that data in an arraylist then pass it to the php file for further processing. I am able to do this for single values but for multiple values I don't have any idea.

This is my adapter class:

public class ListAdapter extends ArrayAdapter {

//static String get_empcode,get_empname,get_app_date,get_nod,get_from,get_to,get_lv_type,get_reason,get_remark,get_status;
static List list=new ArrayList();
static String[]  get_empcode,get_empname,get_app_date,get_nod,get_from,get_to,get_lv_type,get_reason,get_remark,get_status;
static String sting_length;
int i;

public ListAdapter(Context context, int resource) {
    super(context, resource);       
}

public void add(Details object) {       
    super.add(object);
    list.add(object);
}

public int getCount() {     
    return list.size();
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) { 
     View row;
    row=convertView;
    final DetailsHolder detailsHolder;

    if(row==null)
    {
        LayoutInflater layoutInflater=(LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=layoutInflater.inflate(R.layout.list_layout_approval, parent, false);
        detailsHolder=new DetailsHolder();
        detailsHolder.empcode=(TextView) row.findViewById(R.id.empcode1);
        detailsHolder.empname=(TextView) row.findViewById(R.id.empname1);
        detailsHolder.appdate=(TextView) row.findViewById(R.id.appdate1);
        detailsHolder.lv_type=(TextView) row.findViewById(R.id.lv_type1);
        detailsHolder.from=(TextView) row.findViewById(R.id.from1);
        detailsHolder.to=(TextView) row.findViewById(R.id.to1);
        detailsHolder.nod=(TextView) row.findViewById(R.id.nod1);
        detailsHolder.reason=(TextView) row.findViewById(R.id.reason1);
        detailsHolder.status=(TextView) row.findViewById(R.id.status1);
        detailsHolder.cb2=(CheckBox) row.findViewById(R.id.cb2);


       // Leave_approval la=new Leave_approval();
           /*
            if( Leave_approval.cb_status==true){
                detailsHolder.cb2.setChecked(true);
            }
            else
            {
                detailsHolder.cb2.setChecked(false);
            }
            */

        DetailsHolder.cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(DetailsHolder.cb2.isChecked())
                {
                get_empcode=(String) detailsHolder.empcode.getText();
                get_from=(String) detailsHolder.from.getText();
                get_to=(String) detailsHolder.to.getText();
                get_lv_type=(String) detailsHolder.lv_type.getText();
                get_nod=(String) detailsHolder.nod.getText();
                get_status=(String) detailsHolder.status.getText();                   
                }
           }

        });                 
        row.setTag(detailsHolder);
    }
    else{
        detailsHolder=(DetailsHolder) row.getTag();
    }

    Details details=(Details)this.getItem(position);
    detailsHolder.empcode.setText(details.getEmpcode());
    detailsHolder.empname.setText(details.getEmpname());
    detailsHolder.appdate.setText(details.getApplyDate());
    detailsHolder.lv_type.setText(details.getLeave_type());
    detailsHolder.from.setText(details.getFrom());
    detailsHolder.to.setText(details.getTo());
    detailsHolder.nod.setText(details.getNod());
    detailsHolder.reason.setText(details.getReason());
    detailsHolder.status.setText(details.getStatus());
    return row;
}

here is my Activity file from where I am passing data to php using async task

public class Leave_approval extends Activity {

String JSON_STRING,php_result,LeaveType,empcode,FullName,ApplyDate,From,To,NOD,Reason,Status;
//String empcode_val="",lv_type_val="",nod_value="",status_val="",from_value="",to_value="";
String[] empcode_val,lv_type_val,nod_value,status_val,from_value,to_value;
JSONObject jsonObject;
JSONArray jsonArray;
String[] date_list;
ArrayList<HashMap<String, String>> personList;
TextView head;
ListAdapter listAdapter;
ListView listView;
CheckBox cb1;
static Boolean cb_status;
int i;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.leave_approval);
    listView=(ListView) findViewById(R.id.list);
    cb1=(CheckBox) findViewById(R.id.cb1);

    listAdapter=new ListAdapter(this, R.layout.list_layout_approval);
    listView.setAdapter(listAdapter);

    head=(TextView) findViewById(R.id.head);
     Bundle b = getIntent().getExtras();

     php_result=b.getString("json_data");

    //Toast.makeText(getBaseContext(), php_result, Toast.LENGTH_SHORT).show();

    cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {






     try {
            jsonObject=new JSONObject(php_result);
            jsonArray=jsonObject.getJSONArray("server_response");

            for(int i=0;i<jsonArray.length();i++){
                JSONObject c = jsonArray.getJSONObject(i);
                LeaveType=c.getString("LeaveType");
                empcode=c.getString("empcode");
                FullName=c.getString("FullName");
                ApplyDate=c.getString("ApplyDate");
                From=c.getString("From");
                To=c.getString("To");
                NOD=c.getString("NOD");
                Reason=c.getString("Reason");
                Status=c.getString("Status");

                String[] from_array=From.split("-");
                String[] to_array=To.split("-");
                String[] apply_array=ApplyDate.split("-");
                String from_date,to_date,appdate;


                if(from_array[1].equals("01"))
                    from_array[1]="jan";

                if(from_array[1].equals("02"))
                    from_array[1]="Feb";

                if(from_array[1].equals("03"))
                    from_array[1]="Mar";

                if(from_array[1].equals("04"))
                    from_array[1]="Apr";

                if(from_array[1].equals("05"))
                    from_array[1]="May";

                if(from_array[1].equals("06"))
                    from_array[1]="Jun";

                if(from_array[1].equals("07"))
                    from_array[1]="Jul";

                if(from_array[1].equals("08"))
                    from_array[1]="Aug";

                if(from_array[1].equals("09"))
                    from_array[1]="Sep";

                if(from_array[1].equals("10"))
                    from_array[1]="Oct";

                if(from_array[1].equals("11"))
                    from_array[1]="Nov";


                if(from_array[1].equals("12"))
                    from_array[1]="Dec";



                if(to_array[1].equals("01"))
                    to_array[1]="jan";

                if(to_array[1].equals("02"))
                    from_array[1]="Feb";

                if(to_array[1].equals("03"))
                    to_array[1]="Mar";

                if(to_array[1].equals("04"))
                    to_array[1]="Apr";

                if(to_array[1].equals("05"))
                    to_array[1]="May";

                if(to_array[1].equals("06"))
                    to_array[1]="Jun";

                if(to_array[1].equals("07"))
                    to_array[1]="Jul";

                if(to_array[1].equals("08"))
                    to_array[1]="Aug";

                if(to_array[1].equals("09"))
                    to_array[1]="Sep";

                if(to_array[1].equals("10"))
                    to_array[1]="Oct";

                if(to_array[1].equals("11"))
                    to_array[1]="Nov";


                if(to_array[1].equals("12"))
                    to_array[1]="Dec";




                if(apply_array[1].equals("01"))
                    apply_array[1]="jan";

                if(apply_array[1].equals("02"))
                    apply_array[1]="Feb";

                if(apply_array[1].equals("03"))
                    apply_array[1]="Mar";

                if(apply_array[1].equals("04"))
                    apply_array[1]="Apr";

                if(apply_array[1].equals("05"))
                    apply_array[1]="May";

                if(apply_array[1].equals("06"))
                    apply_array[1]="Jun";

                if(apply_array[1].equals("07"))
                    apply_array[1]="Jul";

                if(apply_array[1].equals("08"))
                    apply_array[1]="Aug";

                if(apply_array[1].equals("09"))
                    apply_array[1]="Sep";

                if(apply_array[1].equals("10"))
                    apply_array[1]="Oct";

                if(apply_array[1].equals("11"))
                    apply_array[1]="Nov";


                if(apply_array[1].equals("12"))
                    apply_array[1]="Dec";

                from_date=from_array[2]+"-"+from_array[1]+"-"+from_array[0];
                to_date=to_array[2]+"-"+to_array[1]+"-"+to_array[0];
                appdate=apply_array[2]+"-"+apply_array[1]+"-"+apply_array[0];

                Details details=new Details(empcode,FullName,appdate,LeaveType,from_date,to_date,NOD,Reason,Status);

                listAdapter.add(details);

              }




           } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

public void approve(View view){

    String empcode_val="",lv_type_val="",nod_value="",status_val="",from_value="",to_value="";

    empcode_val=ListAdapter.get_empcode;
    lv_type_val=ListAdapter.get_lv_type;
    nod_value=ListAdapter.get_nod;
    status_val="Approved";
    from_value=ListAdapter.get_from;
    to_value=ListAdapter.get_to;   


    if(empcode_val.equals("")&&lv_type_val.equals("")&&nod_value.equals("")&&status_val.equals("")&&from_value.equals("")
            &&to_value.equals("")){

        Toast.makeText(getBaseContext(), "Please make some selections",Toast.LENGTH_SHORT).show();
    }

    else{

    BackgroundTask backgroundTask = new BackgroundTask(getApplicationContext());
    backgroundTask.execute(empcode_val,lv_type_val,nod_value,status_val,from_value,to_value);

    }

}

public class BackgroundTask extends AsyncTask {

     AsyncResponse delegate = null;

     Context ctx;  
       BackgroundTask(Context ctx)  
       {  
        this.ctx =ctx;  
       }


       @Override  
       protected void onPreExecute() {  

       }  

       protected String doInBackground(String... params) {  


          String login_url = "http://10.0.2.2/neha/leave_approval_update.php";

           String empcode = params[0];
           String leave_type=params[1];
           String nod=params[2];
           String status=params[3];
           String from=params[4];
           String to=params[5];

               try {  
                 URL url = new URL(login_url);  
                 HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();  
                 httpURLConnection.setRequestMethod("POST");  
                 httpURLConnection.setDoOutput(true);  
                 httpURLConnection.setDoInput(true);  
                 OutputStream outputStream = httpURLConnection.getOutputStream();  
                 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));  
                 String data = URLEncoder.encode("empcode","UTF-8")+"="+URLEncoder.encode(empcode,"UTF-8")+"&"+  
                                URLEncoder.encode("leave_type","UTF-8")+"="+URLEncoder.encode(leave_type,"UTF-8")+"&"+  
                                URLEncoder.encode("nod","UTF-8")+"="+URLEncoder.encode(nod,"UTF-8")+"&"+  
                                URLEncoder.encode("status","UTF-8")+"="+URLEncoder.encode(status,"UTF-8")+"&"+  
                                URLEncoder.encode("from","UTF-8")+"="+URLEncoder.encode(from,"UTF-8")+"&"+  
                                URLEncoder.encode("to","UTF-8")+"="+URLEncoder.encode(to,"UTF-8");          

                 bufferedWriter.write(data);  
                 bufferedWriter.flush();  
                 bufferedWriter.close();  
                 outputStream.close();  
                 InputStream inputStream = httpURLConnection.getInputStream();  
                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));  
                 String response = "";  
                 String line = "";  
                 while ((line = bufferedReader.readLine())!=null)  
                 {  
                   response+= line;  
                 }  
                 bufferedReader.close();  
                 inputStream.close();  
                 httpURLConnection.disconnect();  
                 return response;  
               } catch (MalformedURLException e) {  
                 e.printStackTrace();  
               } catch (IOException e) {  
                 e.printStackTrace();  
               }  
            // }  
             return null;  
           }  
           @Override  
           protected void onProgressUpdate(Void... values) {  
             super.onProgressUpdate(values);  
           }  
           @Override  
           protected void onPostExecute(String result) {  

                 Toast.makeText(ctx, result, Toast.LENGTH_SHORT).show();


            }  






}

}

5
  • The code you posted doesn't show how you are making it work for a single list. Commented Feb 4, 2016 at 17:34
  • I have edited my question Commented Feb 4, 2016 at 17:40
  • See where you are building a string of URLEncoded data to write to the output stream in your connection? Instead of just writing the data for that individual object to be posted, you need to modify the data string to contain an array of all of the objects "data" strings that you want to pass. Or utilize a library that will make all of this easier for you (Volley, GSON, etc.) Commented Feb 4, 2016 at 17:46
  • can u plz give me little idea for implementing this in my code? Commented Feb 4, 2016 at 17:56
  • Take a look at stackoverflow.com/questions/9117532/… Commented Feb 4, 2016 at 18:04

1 Answer 1

0

Ok, I got solved my problem by creating array list for all my data then I parse then into json array and passed it as a string to php. This is how I did:

these are my arraylist

public void approve(View view){

    ArrayList<String> emplist=new ArrayList<>();
    ArrayList<String> fromlist=new ArrayList<>();
    ArrayList<String> tolist=new ArrayList<>();
    ArrayList<String> lv_typelist=new ArrayList<>();
    ArrayList<String> nodlist=new ArrayList<>();
    ArrayList<String> status=new ArrayList<>();

    // getting data from ListAdapter class and and adding it to arraylist

   emplist=ListAdapter.getemplist();
    fromlist=ListAdapter.getfromlist();
    tolist=ListAdapter.gettolist();
    lv_typelist=ListAdapter.getlv_typelist();
    nodlist=ListAdapter.getnodlist();
    status.add("Approved");





    JSONArray jArr1= new JSONArray();
    for(String data:emplist)
     {
       jArr1.put(data);
     }


    JSONArray jArr2= new JSONArray();
    for(String data:fromlist)
     {
       jArr2.put(data);
     }

    JSONArray jArr3= new JSONArray();
    for(String data:tolist)
     {
       jArr3.put(data);
     }

    JSONArray jArr4= new JSONArray();
    for(String data:lv_typelist)
     {
       jArr4.put(data);
     }

    JSONArray jArr5= new JSONArray();
    for(String data:status)
     {
       jArr5.put(data);
     }

    JSONArray jArraySet = new JSONArray();
      jArraySet.put(jArr1);
      jArraySet.put(jArr2);
      jArraySet.put(jArr3);
      jArraySet.put(jArr4);
      jArraySet.put(jArr5);



    BackgroundTask backgroundTask = new BackgroundTask(getApplicationContext());
    backgroundTask.execute(json_string);



}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.