在使用intent和sqlite之后,如何从listview中获取项目名称?

vfhzx4xs  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(240)

我有两个列表视图,每一个都在不同的活动我想发送一个项目从第一个列表视图到另一个通过长时间点击它我使用意图,我使用字符串文件获得数据它发送项目完美,但它没有保存到sqlite我不知道哪里是问题请看我的代码

First Activity(Main Activity):
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long l) {
                    Intent intent = new Intent(MainActivity.this, cc.class);
                    intent.putExtra("EXTRAKEY_ID",l);// THIS ADDED
                    startActivity(intent);
                    fav_name = getAllDataInCurrentLocale.getString(getAllDataInCurrentLocale.getColumnIndex(FAVOURITES_COL_NAME));
                    Toast.makeText(MainActivity.this, fav_name+" Added To Favorite", Toast.LENGTH_SHORT).show();

                    return true;
                }

            });
Second Activity(cc):
                fav_id = getIntent().getLongExtra("EXTRAKEY_ID", 0);

        manageFavouritesListView();

if (fav_id==0){

}

        getDataInCurrentLocaleById = dbSqlite.getDataInCurrentLocaleById(this,fav_id);
            if (getDataInCurrentLocaleById.moveToFirst()) {
                fav_name = getDataInCurrentLocaleById.getString(getDataInCurrentLocaleById.getColumnIndex(FAVOURITES_COL_NAME));

            }

        getDataInCurrentLocaleById.close();
        }

    private void manageFavouritesListView() {
        getDataInCurrentLocaleById = dbSqlite.getDataInCurrentLocaleById(this,fav_id);
        if (favourites_adapter == null) {
            favourites_adapter = new SimpleCursorAdapter(
                    this,
                    R.layout.textview2,
                    getDataInCurrentLocaleById,
                    new String[]{FAVOURITES_COL_NAME},
                    new int[]{R.id.textview10},
                    0
            );
            listView.setAdapter(favourites_adapter);
            setListViewHandler(listView, true);
        } else {
            favourites_adapter.swapCursor(getDataInCurrentLocaleById);
        }
    }
Db_Sqlite:
    public Cursor getAllDataInCurrentLocale(Context context) {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor csr = db.query(TABLE_FAVOURITES,null,null,null,null,null,null);
        if (csr.getCount() < 1) return csr;
        MatrixCursor mxcsr = new MatrixCursor(csr.getColumnNames(),csr.getCount());
        while (csr.moveToNext()) {
            mxcsr.addRow(convertCursorRow(context,csr,new String[]{FAVOURITES_COL_NAME}));
        }
        csr.close();
        return mxcsr;
    }
    public Cursor getDataInCurrentLocaleById(Context context, long id) {
        SQLiteDatabase db = this.getWritableDatabase();
        String wherepart = FAVOURITES_COL_ID + "=?";
        String[] args = new String[]{String.valueOf(id)};
        Cursor csr = db.query(TABLE_FAVOURITES,null,wherepart,args,null,null,null);
        if (csr.getCount() < 1) return csr;
        MatrixCursor mxcsr = new MatrixCursor(csr.getColumnNames(),csr.getCount());
        while (csr.moveToNext()) {
            mxcsr.addRow(convertCursorRow(context,csr,new String[]{FAVOURITES_COL_NAME}));
        }
        csr.close();
        return mxcsr;
    }

    /* This getting columns from Cursor into String array (no BLOB handleing)*/
    public String[] convertCursorRow(Context context, Cursor csr, String[] columnsToConvert) {
        String[] rv = new String[csr.getColumnCount()];
        for (String s: csr.getColumnNames()) {
            boolean converted = false;
            for (String ctc: columnsToConvert) {
                if (csr.getType(csr.getColumnIndex(s)) == Cursor.FIELD_TYPE_BLOB) {
                    //........ would have to handle BLOB here if needed (another question if needed)
                }
                if (ctc.equals(s)) {
                    rv[csr.getColumnIndex(s)] = StringResourcesHandling.getStringByName(context,csr.getString(csr.getColumnIndex(s)));
                    converted = true;
                }
            } if (!converted) {
                rv[csr.getColumnIndex(s)] = csr.getString(csr.getColumnIndex(s));
            }
        }
        return rv;
    }

    }

我不知道哪里有问题请帮我提前谢谢

tez616oj

tez616oj1#

You should notify the adapter that data changed.

    private void manageFavouritesListView() {
            getDataInCurrentLocaleById = dbSqlite.getDataInCurrentLocaleById(this,fav_id);
            if (favourites_adapter == null) {
                favourites_adapter = new SimpleCursorAdapter(
                        this,
                        R.layout.textview2,
                        getDataInCurrentLocaleById,
                        new String[]{FAVOURITES_COL_NAME},
                        new int[]{R.id.textview10},
                        0
                );
                listView.setAdapter(favourites_adapter);
                favourites_adapter.notifyDataSetChanged();
                setListViewHandler(listView, true);
            } else {
                favourites_adapter.swapCursor(getDataInCurrentLocaleById);
            }
        }

相关问题