单击列表项时,我收到ClasscastException。我想通过点击listitem移动到详细信息屏幕。
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)
不知道为什么我收到此错误。请帮忙。
答案 0 :(得分:0)
在convertView.setTag(mlr);
之前的getView函数return convertView;
中
这是因为您在setTag中使用ViewHolder对象并将其转换为getTag上的MLeaveRequests对象
<强> -------------------------------------------- --- EDITED ---------------------------------------------- - 强>
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;
}