将对象的arraylist传递给另一个活动

时间:2013-09-30 13:24:16

标签: android listview arraylist android-arrayadapter parcelable

我正在尝试将对象的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)

1 个答案:

答案 0 :(得分:0)

该行

holder = (ViewHolder) convertView.getTag();

为什么要调用getTag()?我想如果你只是做

holder = (ViewHolder) convertView;

它应该有用。