简体   繁体   English

将意图与sqlite一起使用后如何从列表视图中获取项目名称?

[英]how to get the items name from listview after using intent with sqlite?

i have two listviews each one of them is in different activity i want to send an item from the first listview to the other one by long click on it im using intent, im using string file to get the data it sends the item perfectly but its not saved to sqlite i dont know where is the problem please take a look at my code我有两个列表视图,每个列表视图都在不同的活动中 我想通过长按它来将一个项目从第一个列表视图发送到另一个列表视图我使用意图,我使用字符串文件来获取它完美发送该项目的数据,但它的没有保存到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;
    }

    }

i dont where is the problem please help me thank you in advance我不知道问题出在哪里请帮助我提前谢谢

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);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在将电话中的所有联系人填充到列表视图之后,如何获取选定的联系人姓名(将其传递到意图)? - How to get the selected Contact Name (to pass it into an intent) after populating all contacts from phone into a listview? 如何使用OnItemLongClickListener()从SQLite创建的列表视图中删除项目? - How to delete items from listview created from SQLite using OnItemLongClickListener()? 如何在文本视图中显示获取意图的项目 - how to display items from a get intent in textviews 如何从 ListView 获取项目并将这些项目转换为 ArrayList? - How can I get the items from a ListView and convert these items to an ArrayList? 如何从Android中的SQLite数据库获取名称 - How to get name from SQLite Database in android 如何获取列表视图项的ID? 我正在使用Firebase数据库 - How to get the Id of listview items? I am using Firebase database 如何在ListView(包含来自sqlite数据库的项目)中使CheckBox正常工作 - How to make working CheckBox inside ListView (which has items from sqlite database) 如何使用存储在 SQLite 中的图像名称从可绘制对象中获取图像? - How can i get an image from drawable using image name stored in SQLite? Android:从Intent获取App-Name - Android: Get App-Name from Intent 如何使用 Sqlite 从数据库中获取数据? - how to get data from database using Sqlite?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM