使用sqlite意图后如何从listview中获取项目名称?

Posted

技术标签:

【中文标题】使用sqlite意图后如何从listview中获取项目名称?【英文标题】:how to get the items name from listview after using intent with sqlite? 【发布时间】:2020-05-29 08:02:10 【问题描述】:

我有两个列表视图,每个列表视图都处于不同的活动中完美,但它没有保存到 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;
    

    

我不知道问题出在哪里,请帮助我提前谢谢你

【问题讨论】:

【参考方案1】:
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);
            
        

【讨论】:

感谢您的回复,但我尝试了您的方法,但没有成功

以上是关于使用sqlite意图后如何从listview中获取项目名称?的主要内容,如果未能解决你的问题,请参考以下文章

Android Sqlite:如何从 Listview 中获取 SQLite rowId?

Android:SQLite 和 ListViews

从 ListView(SQLite 数据库)中长按行删除

如何获取listView位置值并通过intent传递给另一个类

开始新活动后,ListView 会减少

如何将数据从 listview 单选按钮保存到 SQLite?