如何将多个ArrayList从android传递到php

问题描述:

我正在处理一个包含动态数据的自定义listview的项目。我想要的是在arraylist中获取这些数据,然后将其传递给php文件进行进一步处理。我能够为单个值做到这一点,但对于多个值,我没有任何想法。如何将多个ArrayList从android传递到php

这是我的适配器类:

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

这里是从哪儿我传递数据使用异步任务到PHP我的活动文件

公共类Leave_approval延伸活动{

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

    } 

} 

公共类BackgroundTask扩展的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(); 


      } 






} 

}

+0

您发布不显示你是如何使之成为一个单独的列表工作的代码。 – zgc7009

+0

我编辑了我的问题 – Nks

+0

查看您在哪里构建一串URLEncoded数据以写入连接中的输出流?您不需要为要发布的单个对象编写数据,而需要修改数据字符串以包含要传递的所有对象“数据”字符串的数组。或者利用一个能让你更轻松的库(Volley,GSON等) – zgc7009

好吧,我被创建数组列表,解决了我的问题,我所有的数据,那么我解析然后进入JSON数组,并通过它作为一个字符串到PHP 。 这是我做的事:

这些都是我的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); 



}