我有一个名为GridViewOrdersCustomAdapter的gridView自定义适配器,它扩展了BaseAdapter。它填充并更新得很好但是当我单击gridView的一个元素时,当我尝试将元素转换为TableObject时,我得到了一个ClassCastException。似乎getView返回一个整数而不是所需的对象。
以下是我实现GridViewOrdersCustomAdapter的方法:
public class GridViewOrdersCustomAdapter extends BaseAdapter {
Context context;
int layoutResourceId;
ArrayList<TableObject> tables = new ArrayList<TableObject>();
public GridViewOrdersCustomAdapter(Context context, int layoutResourceId, ArrayList<TableObject> tables, String tableZone) {
this.layoutResourceId=layoutResourceId;
this.context=context;
this.tables.addAll(filterTables(tables, tableZone));
}
@Override
public int getCount() {
return tables.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView textView;
ImageView imageItem;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder;
View rowView = convertView;
TableObject table = this.tables.get(position);
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(this.layoutResourceId, parent, false);
holder = new Holder();
holder.imageItem = (ImageView) rowView.findViewById(R.id.dishImage);
holder.textView = (TextView) rowView.findViewById(R.id.textViewTableInformation);
rowView.setTag(holder);
}
else {
holder = (Holder) rowView.getTag();
}
if (table.getStatus().equals(TableObject.TABLE_FREE)) {
holder.imageItem.setImageResource(R.drawable.ic_table_free_4);
}
else if (table.getStatus().equals(TableObject.TABLE_OCCUPIED)) {
holder.imageItem.setImageResource(R.drawable.ic_table_occ_4);
}
else if (table.getStatus().equals(TableObject.TABLE_MERGED)) {
holder.imageItem.setImageResource(R.drawable.ic_table_merged_4);
}
holder.textView.setText(table.getId());
return rowView;
}
public void updateContent (ArrayList<TableObject> tables, String tableZone) {
this.tables.addAll(filterTables(tables, tableZone));
this.notifyDataSetChanged();
}
private ArrayList<TableObject> filterTables (ArrayList<TableObject> receivedTables, String tableZone) {
ArrayList<TableObject> tables = new ArrayList<TableObject>();
for (int index = 0; index<receivedTables.size();index++) {
if (receivedTables.get(index).getTableZone().equals(tableZone)) {
tables.add(receivedTables.get(index));
}
}
return tables;
}
}
这是我收到错误的地方:
...
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
final TableObject table = (TableObject) parent.getItemAtPosition(position);
这就是错误:
03-17 10:08:06.211 4077-4077/com.example.hotelbaiaazzurramaintablet E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: java.lang.Integer cannot be cast to com.example.hotelbaiaazzurramaintablet.objects.TableObject
at com.example.hotelbaiaazzurramaintablet.activities.MainActivityTerrace$2.onItemClick(MainActivityTerrace.java:147)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
at android.widget.AbsListView$1.run(AbsListView.java:3529)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
enter code here
答案 0 :(得分:2)
ClassCastException:无法强制转换java.lang.Integer
因为从position
类的int
方法返回getItem
GridViewOrdersCustomAdapter
。
而不是position
使用getItem
的参数将ArrayList从ArrayList返回:
@Override
public Object getItem(int position) {
return tables.get(position);
}
在onItemClick
方法中,点击的值为:
final TableObject table =(TableObject) parent.getAdapter().getItem(position);