无法将对象转换为int

时间:2018-10-08 03:35:28

标签: android listview

在构建gradle之后,我将从列表视图中删除特定数据,并显示错误消息“错误:类型不兼容:对象无法转换为int”

DatabaseHelper的代码

public void deletegroce(int groceId){
    String groceid[] = { String.valueOf(groceId) };
    SQLiteDatabase sqdb = this.getWritableDatabase();
    sqdb.delete(TABLE_NAME, KEY_GROCE_ID + " = ?", groceid);
    sqdb.close();
}

这是我的Grocery_List的完整代码,错误位于第65行,“ int dave = aList.get(position); dbhelper.deletegroce(dave);”

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grocery_list);
    lvGorc = (ListView) findViewById(R.id.lvGroc);
    dbhelper = new DatabaseHelper(Grocery_list.this);
    final List aList = dbhelper.getAllGroceries();
    final ArrayAdapter<String> La = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, aList);
    lvGorc.setAdapter(La);


    lvGorc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(final AdapterView<?> adapterView, View view, int i, long l) {
            AlertDialog.Builder adb = new AlertDialog.Builder(Grocery_list.this);
            adb.setTitle("Option");
            adb.setMessage("What do you want to do?");
            adb.setPositiveButton("Update", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });
            adb.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {


                }
            });
            adb.show();
        }
    });

    lvGorc.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
            AlertDialog.Builder adb = new AlertDialog.Builder(Grocery_list.this);
            adb.setTitle("Delete?");
            adb.setMessage("Are you sure you want to delete the selected item?");
            adb.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int dave = aList.get(position);
                    dbhelper.deletegroce(dave);

                    La.notifyDataSetChanged();
                }
            });
            adb.show();
            return  true;
        }
    });
}

2 个答案:

答案 0 :(得分:1)

您定义的aListList个对象。 ArrayAdapter<String>接受所有类型的对象列表,它将调用所有对象都拥有的toString()方法,以将其显示在ListView中。

当您尝试获取aList的对象时,它将返回该索引的对象,该对象无法转换为int

我认为您可以简单地将对象的id传递给dbhelper.deletegroce(dave)。如果您的aListList个对象的Integer,则可以通过调用以下命令将其强制转换为int

int dave = (int) aList.get(position);

答案 1 :(得分:0)

您在OnCreate方法中显示的第五行  最终列表aList = dbhelper.getAllGroceries();

它没有指定aList是整数列表。您的getAllGroceries返回什么? 您还应该将列表定义为最终列表aList = dbhelper.getAllGroceries();。 并确保dbhelper.getAllGroceries()返回整数数据列表。