BaseAdapter 获取 classcastexception onclick

BaseAdapter get classcastexception onclick

我有一个名为 GridViewOrdersCustomAdapter 的 gridView 自定义适配器,它扩展了 BaseAdapter。它可以很好地填充和更新,但是当我单击 gridView 的一个元素时,当我尝试将该元素转换为 TableObject 时出现 ClassCastException。 getView returns 似乎是一个整数而不是所需的对象。

这是我实现 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.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.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

ClassCastException: java.lang.Integer cannot be cast

因为 returning position 这是 int 来自 GridViewOrdersCustomAdapter getItem 方法 class.

而不是 ArrayList 中的 position return 项使用 getItem 的参数作为:

@Override
public Object getItem(int position) {
    return tables.get(position);   
}

并且在onItemClick方法中获得点击值:

final TableObject table =(TableObject) parent.getAdapter().getItem(position);