0

单击列表项时出现 ClasscastException。我想通过单击列表项移动到详细信息屏幕。

Here is my code:    

private MyCustomAdapter dataAdapter = null;
     private View currentView = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.manager_leave_requests_list);
        displayListView();
    }

    private void displayListView()
    {
        ArrayList<MLeaveRequests> lrList = new ArrayList<MLeaveRequests>();

        MLeaveRequests requests = new MLeaveRequests("12345678", "Shyam",
                "09/29/2013", "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ");
        lrList.add(requests);
        requests = new MLeaveRequests("22345678", "Shyam", "09/29/2013",
                "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ");
        lrList.add(requests);
        requests = new MLeaveRequests("32345678", "Shyam", "09/29/2013",
                "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ");
        lrList.add(requests);
        requests = new MLeaveRequests("42345678", "Shyam", "09/29/2013",
                "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ");
        lrList.add(requests);
        requests = new MLeaveRequests("52345678", "Shyam", "09/29/2013",
                "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ");
        lrList.add(requests);
        requests = new MLeaveRequests("62345678", "Shyam", "09/29/2013",
                "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ");
        lrList.add(requests);
        requests = new MLeaveRequests("72345678", "Shyam", "09/29/2013",
                "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ");
        lrList.add(requests);

        //create an ArrayAdaptar 
          dataAdapter = new MyCustomAdapter(this, R.layout.manager_leave_requests_list_item, lrList);

         ListView list = (ListView) findViewById(R.id.mngrLeaveRequestsList) ;
         list.setAdapter(dataAdapter);

         list.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView<?> parent, View view,
                 int position, long id) {

                currentView = view;

                //get reference to the MLeaveRequests Object

                 MLeaveRequests requests = (MLeaveRequests) view.getTag();

                Intent reqDetails = new Intent(ManagerLeaveRequestsList.this,LeaveRequestDetails.class);
                Bundle b = new Bundle();
                //pass the country object as a parcel
                b.putParcelable("Request", requests);
                reqDetails.putExtras(b);
                startActivity(reqDetails);

               }
              });
    }
    private class MyCustomAdapter extends ArrayAdapter<MLeaveRequests>{
        private ArrayList<MLeaveRequests> leaves;


        public MyCustomAdapter(
                Context context,
                int managerLeaveRequestsListItem,
                ArrayList<MLeaveRequests> lrList) {
            // TODO Auto-generated constructor stub
            super(context, managerLeaveRequestsListItem, lrList);
            this.leaves = lrList;

        }
        public class ViewHolder{
            TextView requestID = null;
            TextView appliedBy = null;
            TextView appliedOn = 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.manager_leave_requests_list_item, null);
             holder = new ViewHolder();
             holder.requestID = (TextView) convertView.findViewById(R.id.txtMLRID);
             holder.appliedBy = (TextView) convertView.findViewById(R.id.txtMLRAppliedBy);
             holder.appliedOn = (TextView) convertView.findViewById(R.id.txtMLRAppliedOn);
             convertView.setTag(holder);
            }
            else
                holder = (ViewHolder) convertView.getTag();

            final MLeaveRequests mlr = leaves.get(position);
            if(mlr != null){
                mlr.setListPosition(position);
                holder.requestID.setText(mlr.getRequestID());
                holder.appliedBy.setText(mlr.getAppliedBy());
                holder.appliedOn.setText(mlr.getAppliedOn());

            }

            return convertView;
          }

    }

以下是来自 logcat 的条目:

09-29 15:49:10.942: ERROR/AndroidRuntime(487): FATAL EXCEPTION: main
09-29 15:49:10.942: ERROR/AndroidRuntime(487): java.lang.ClassCastException: com.XXXXXX.XXXXXXXXXX.ManagerLeaveRequestsList$MyCustomAdapter$ViewHolder
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at com.XXXXXXXX.XXXXXXXXXX.ManagerLeaveRequestsList$1.onItemClick(ManagerLeaveRequestsList.java:75)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at android.widget.ListView.performItemClick(ListView.java:3382)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at android.os.Handler.handleCallback(Handler.java:587)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at android.os.Looper.loop(Looper.java:123)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at java.lang.reflect.Method.invokeNative(Native Method)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at java.lang.reflect.Method.invoke(Method.java:521)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-29 15:49:10.942: ERROR/AndroidRuntime(487):     at dalvik.system.NativeStart.main(Native Method)

不知道为什么我会收到此错误。请帮忙。

4

1 回答 1

0

在您的 getView 函数convertView.setTag(mlr);之前return convertView; 它发生是因为您在 setTag 中使用 ViewHolder 对象并将其转换为 getTag 上的 MLeaveRequests 对象

----------------------------------------------------------- 已编辑 -- ---------------------------------------------

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

     LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     convertView = li.inflate(R.layout.manager_leave_requests_list_item, null);
     holder = new ViewHolder();
     holder.requestID = (TextView) convertView.findViewById(R.id.txtMLRID);
     holder.appliedBy = (TextView) convertView.findViewById(R.id.txtMLRAppliedBy);
     holder.appliedOn = (TextView) convertView.findViewById(R.id.txtMLRAppliedOn);

    final MLeaveRequests mlr = leaves.get(position);
    convertView.setTag(mlr);
    if(mlr != null){
        mlr.setListPosition(position);
        holder.requestID.setText(mlr.getRequestID());
        holder.appliedBy.setText(mlr.getAppliedBy());
        holder.appliedOn.setText(mlr.getAppliedOn());
    }

    return convertView;
}
于 2013-09-29T10:51:33.107 回答