Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在将intent与sqlite一起使用后,如何从listview中获取项目名称?_Java_Android_Sqlite_Listview - Fatal编程技术网

Java 在将intent与sqlite一起使用后,如何从listview中获取项目名称?

Java 在将intent与sqlite一起使用后,如何从listview中获取项目名称?,java,android,sqlite,listview,Java,Android,Sqlite,Listview,我有两个列表视图,每一个都处于不同的活动中我想通过长时间单击将一个项目从第一个列表视图发送到另一个列表视图我使用intent,我使用字符串文件获取数据它完美地发送了该项目,但未保存到sqlite我不知道问题出在哪里请查看我的代码 First Activity(Main Activity): listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Overrid

我有两个列表视图,每一个都处于不同的活动中我想通过长时间单击将一个项目从第一个列表视图发送到另一个列表视图我使用intent,我使用字符串文件获取数据它完美地发送了该项目,但未保存到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;
                }

            });
Db_Sqlite:
公共游标getAllDataInCurrentLocale(上下文){
SQLiteDatabase db=this.getWritableDatabase();
游标csr=db.query(表_收藏夹,null,null,null,null,null,null);
if(csr.getCount()<1)返回csr;
MatrixCursor mxcsr=新MatrixCursor(csr.getColumnNames(),csr.getCount());
while(csr.moveToNext()){
addRow(convertCursorRow(context,csr,新字符串[]{favorites\u COL\u NAME}));
}
csr.close();
返回mxcsr;
}
公共游标getDataInCurrentLocaleById(上下文上下文,长id){
SQLiteDatabase db=this.getWritableDatabase();
字符串,其中part=favorites\u COL\u ID+“=?”;
字符串[]args=新字符串[]{String.valueOf(id)};
游标csr=db.query(表_收藏夹,null,wherepart,args,null,null,null);
if(csr.getCount()<1)返回csr;
MatrixCursor mxcsr=新MatrixCursor(csr.getColumnNames(),csr.getCount());
while(csr.moveToNext()){
addRow(convertCursorRow(context,csr,新字符串[]{favorites\u COL\u NAME}));
}
csr.close();
返回mxcsr;
}
/*将列从游标获取到字符串数组(无BLOB句柄)*/
公共字符串[]convertCursorRow(上下文上下文、光标csr、字符串[]columnsToConvert){
字符串[]rv=新字符串[csr.getColumnCount()];
对于(字符串s:csr.getColumnNames()){
布尔值=假;
for(字符串ctc:columnsToConvert){
if(csr.getType(csr.getColumnIndex))==Cursor.FIELD\u TYPE\u BLOB){
//……如果需要,必须在此处处理BLOB(如果需要,另一个问题)
}
如果(ctc.等于){
rv[csr.getColumnIndex]=StringResourcesHandling.getStringByName(上下文,csr.getString(csr.getColumnIndex));
转换=真;
}
}如果(!已转换){
rv[csr.getColumnIndex]=csr.getString(csr.getColumnIndex));
}
}
返回rv;
}
}

我不知道问题出在哪里请帮我提前谢谢你的回复但我尝试了你的方法但没有成功
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);
            }
        }
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;
    }

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