2013-09-30 127 views
-1

我試圖從一個活動傳遞對象的ArrayList到另一個活動象下面這樣:傳遞對象的ArrayList到另一個活動

Intent searchOk = new Intent(EmployeeSearch.this, EmployeeSearchResults.class); 
         //searchOk.p 
         searchOk.putParcelableArrayListExtra("Employees", searchEmps); 
         startActivity(searchOk);  

I have created one POJO class that implements parcelable interface like below : 

public class SearchedEmployees implements Parcelable{ 

    String empID = ""; 
    String empFName = ""; 
    String empLName = ""; 
    int listPosition = 0; 

    public SearchedEmployees(String empID, String empFName, String empLName) 
    { 
     super(); 
     this.empID = empID; 
     this.empFName = empFName; 
     this.empLName = empLName; 
    } 

    public String getEmpID() { 
     return empID; 
    } 

    public void setEmpID(String empID) { 
     this.empID = empID; 
    } 

    public String getEmpFName() { 
     return empFName; 
    } 

    public void setEmpFName(String empFName) { 
     this.empFName = empFName; 
    } 

    public String getEmpLName() { 
     return empLName; 
    } 

    public void setEmpLName(String empLName) { 
     this.empLName = empLName; 
    } 

    public int getListPosition() { 
     return listPosition; 
    } 

    public void setListPosition(int listPosition) { 
     this.listPosition = listPosition; 
    } 

    @Override 
    public int describeContents() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     // TODO Auto-generated method stub 
     dest.writeString(this.empID); 
     dest.writeString(this.empFName); 
     dest.writeString(this.empLName); 
    } 

    public static final Parcelable.Creator<SearchedEmployees> CREATOR = new Parcelable.Creator<SearchedEmployees>() { 

     @Override 
     public SearchedEmployees createFromParcel(Parcel in) { 
      // TODO Auto-generated method stub 
      return new SearchedEmployees(in); 
     } 

     @Override 
     public SearchedEmployees[] newArray(int size) { 
      // TODO Auto-generated method stub 
      return new SearchedEmployees[size]; 
     } 
    }; 

    private SearchedEmployees(Parcel in) 
    { 
     this.empID = in.readString(); 
     this.empFName = in.readString(); 
     this.empLName = in.readString(); 
    } 
} 

And in the next activity I have to display the passed arraylist of objects in list view. I am like below: 

private void displayListView(){ 

     Bundle b = getIntent().getExtras(); 
     empsSearched = b.getParcelableArrayList("Employees"); 

     System.out.println("No. of Objects Passed :" + empsSearched.size()); 
     empList = (ListView) findViewById(R.id.empSearchList); 

     //adapter = new CustomAdapter2(EmployeeSearchResults.this, R.layout.employee_search_list_item, empsSearched); 

     dataAdapter = new MyCustomAdapter(this, R.layout.employee_search_list_item, empsSearched); 


     empList.setAdapter(dataAdapter); 

    } 

    private class MyCustomAdapter extends ArrayAdapter<SearchedEmployees>{ 

     private ArrayList<SearchedEmployees> emps; 

     public MyCustomAdapter(Context context, 
       int empSearchListItem, ArrayList<SearchedEmployees> empsSearched) { 
      // TODO Auto-generated constructor stub 
      super(context, empSearchListItem, empsSearched); 
      this.emps = new ArrayList<SearchedEmployees>(); 
      this.emps.addAll(empsSearched); 
      //this.emps = empsSearched; 
      System.out.println("In Adapter: " + emps.size()); 
     } 


     public class ViewHolder{ 
      TextView empID = null; 
      TextView empName = null; 
     } 

     @Override 
      public View getView(int position, View convertView, ViewGroup parent) 
     { 
      ViewHolder holder; 

      if(convertView == null){ 
       LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = li.inflate(R.layout.employee_search_list_item, null); 
       holder = new ViewHolder(); 
       holder.empID = (TextView) convertView.findViewById(R.id.txtEmpSearchEmpID); 
       holder.empName = (TextView) convertView.findViewById(R.id.txtEmpSearchEmpName); 
       convertView.setTag(holder); 
      } 
      else 
       holder = (ViewHolder) convertView.getTag(); 

     final SearchedEmployees semps = emps.get(position); 
     if (semps != null) 
     { 
      semps.setListPosition(position); 
      holder.empID.setText(semps.getEmpID()); 

      holder.empName.setText(semps.getEmpFName() + "" + semps.getEmpLName()); 

     } 

      return convertView; 
     } 
    } 

我在得到一個錯誤:

holder.empID.setText(semps.getEmpID()); 

holder.empName.setText(semps.getEmpFName() + "" + semps.getEmpLName()); 

日誌詳細信息低於:

09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at com.XXXXXX.XXXXXXX.EmployeeSearchResults$MyCustomAdapter.getView(EmployeeSearchResults.java:124) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.widget.AbsListView.obtainView(AbsListView.java:1315) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1198) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.widget.ListView.onMeasure(ListView.java:1109) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.view.View.measure(View.java:8171) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.view.View.measure(View.java:8171) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.view.View.measure(View.java:8171) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.view.View.measure(View.java:8171) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.view.ViewRoot.performTraversals(ViewRoot.java:801) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.os.Handler.dispatchMessage(Handler.java:99) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.os.Looper.loop(Looper.java:123) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at java.lang.reflect.Method.invokeNative(Native Method) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at java.lang.reflect.Method.invoke(Method.java:521) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
    09-30 18:19:13.260: ERROR/AndroidRuntime(1376):  at dalvik.system.NativeStart.main(Native Method) 
+0

給出完整的堆棧跟蹤。確切的線路失敗? System.out.println(「傳遞的對象數量:」+ empsSearched.size()); - 你看到這個輸出嗎?你是否收到新活動中的對象? – agamov

+0

發佈您正在充氣的佈局的xml。 – Jon

+0

是的,我可以看到通過輸出的對象數目。 – user2740599

回答

0

holder = (ViewHolder) convertView.getTag(); 

爲什麼要調用getTag()?我想如果你只是做

holder = (ViewHolder) convertView; 

它應該工作。

相關問題