Android studio 将“活动”中的一行从listview移动到另一个“活动”中的另一个listview

Android studio 将“活动”中的一行从listview移动到另一个“活动”中的另一个listview,android-studio,listview,android-listview,android-sqlite,android-database,Android Studio,Listview,Android Listview,Android Sqlite,Android Database,我有两个listview,它们中的每一个都处于不同的活动中。当我长时间单击第一个listview中的任何一行时,将其移动到第二个listview,并且仍然执行与第一个listview中相同的任务(例如打开活动),并且列表视图从strings.xml获取其数据。这是我的代码,请帮助我 public class DB_Sqlite extends SQLiteOpenHelper { public static final String BDname = "data.db"; pub

我有两个listview,它们中的每一个都处于不同的活动中。当我长时间单击第一个listview中的任何一行时,将其移动到第二个listview,并且仍然执行与第一个listview中相同的任务(例如打开活动),并且列表视图从strings.xml获取其数据。这是我的代码,请帮助我

public class DB_Sqlite extends SQLiteOpenHelper {
    public static final String BDname = "data.db";
    public static final int DBVERSION = 1; /*<<<<< ADDED BUT NOT NEEDED */
    public static final String TABLE_FAVOURITES = "mytable";

    public static final String FAVOURITES_COL_ID = BaseColumns._ID; /*<<<< use the Android stock ID name*/
    public static final String FAVOURITES_COL_NAME = "name";
    public static final String FAVOURITES_COL_FAVOURITEFLAG = "favourite_flag"; /*<<<<< NEW COLUMN */

    public DB_Sqlite(@Nullable Context context) {
        super(context, BDname, null, DBVERSION /*<<<<< used constant above */);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_FAVOURITES + " (" +
                FAVOURITES_COL_ID + " INTEGER PRIMARY KEY," + /*<<<<< AUTOINCREMENT NOT NEEDED AND IS INEFFICIENT */
                FAVOURITES_COL_NAME + " TEXT, " +
                FAVOURITES_COL_FAVOURITEFLAG + " INTEGER DEFAULT 0" + /*<<<<< COLUMN ADDED */
                ")");
        /* CHANGES HERE BELOW loop adding all Resource names NOT VALUES */
        ContentValues cv = new ContentValues();
        for (String s: StringResourcesHandling.getAllStringResourceNames()) {
            cv.clear();
            cv.put(FAVOURITES_COL_NAME,s);
            db.insert(TABLE_FAVOURITES,null,cv);


        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVOURITES);
        onCreate(db);
    }
    public boolean insertData(String name){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(FAVOURITES_COL_NAME, name);
        long result = db.insert(TABLE_FAVOURITES,null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }


    public Cursor getFavouriteRows(boolean favourites)  {
        SQLiteDatabase db = this.getWritableDatabase();
        String whereclause = FAVOURITES_COL_FAVOURITEFLAG + "=?";
        String compare = "<1";
        if (favourites) {
            compare =">0";
        }

        return db.query(
                TABLE_FAVOURITES,null,
                FAVOURITES_COL_FAVOURITEFLAG + compare,
                null,null,null,null
        );
    }


    private int setFavourite(long id, boolean favourite_flag) {
        SQLiteDatabase db = this.getWritableDatabase();
        String whereclause = FAVOURITES_COL_ID + "=?";
        String[] whereargs = new String[]{String.valueOf(id)};
        ContentValues cv = new ContentValues();
        cv.put(FAVOURITES_COL_FAVOURITEFLAG,favourite_flag);
        return db.update(TABLE_FAVOURITES,cv,whereclause,whereargs);
    }


    public int setAsFavourite(long id) {
        return setFavourite(id,true);
    }


    public int setAsNotFavourite(long id) {
        return setFavourite(id, false);
    }

    /* Getting everything and make MatrixCursor VALUES from Resource names from Cursor with Resource names  */
    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)*/
    private 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;
    }

    }
public类DB\u Sqlite扩展SQLiteOpenHelper{
公共静态最终字符串BDname=“data.db”;
公共静态最终int DBVERSION=1;/*问题1
问题一,您正试图将光标强制转换为MatrixCursor,但您无法以这种方式将MatrixCursor强制转换为光标(这是通过getAllDataInCurrentLocale方法完成的)

因此,不是:-

getAllDataInCurrentLocale = (MatrixCursor) dbSqlite.getAllDataInCurrentLocale(this);
你想要:-

getAllDataInCurrentLocale = dbSqlite.getAllDataInCurrentLocale(this);
问题2 第二个问题是,在启动另一个活动时,您从未传递额外的意图

就是

(a)

此处没有设置额外目的(启动tc活动时):-

(b)

同样,此处没有设置额外的意图(启动cc活动时):-

也就是说,您正在实例化名为Cursor的游标对象,然后尝试在名为getAllDataInCurrentLocale的游标上使用游标方法,但该游标尚未实例化。相反,您应该使用实例化游标的方法Cursor

您应该改为使用:-

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

    }
    getAllDataInCurrentLocale.close();
i、 e.声明为getAllDataInCurrentLocale的游标是您应该使用的游标,而不是声明另一个游标

问题4 然后,您将得到一个空指针异常,因为您已将其标记为但已实例化的listView1

因此,您需要一行,如(请参见注释):-


listView1=this.findviewbyd(R.id.listView1);//我完全按照你说的做了,对我来说工作很好,但这不是我想要的,我有三个活动,其中一个是游戏列表(Main),第二个是收藏夹列表(cc),第三个是第一个列表(Main)中的一个游戏,我想要的是,当我长时间单击第一个列表中的任何项目时,该项目将移动到活动(cc)中的第二个列表,如果该项目在第一个列表或第二个列表中,当我按下它时,它将打开第三个活动(tc),如果该项目在第二个列表(cc)中我长时间点击了它,它很好地移回到了(主要活动)中的第一个列表,我希望你明白我的想法,非常感谢你的时间。就像你如何帮助我,但两个列表视图在两个不同的活动中,而不是在同一个(主要活动),(cc活动)cc活动是最受欢迎的活动Activity@AhmedZaqout已经展示的原理是一样的,您只需要根据场景调整ir。
getAllDataInCurrentLocale = (MatrixCursor) dbSqlite.getAllDataInCurrentLocale(this);
getAllDataInCurrentLocale = dbSqlite.getAllDataInCurrentLocale(this);
button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, tc.class);
            startActivity(intent);
        }
    });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i == 0) {
                    Intent intent = new Intent(MainActivity.this, tc.class);
                    startActivity(intent);
                }
            }
        });
Cursor cursor = dbSqlite.getDataInCurrentLocaleById(this, fav_id);
    if (cursor.moveToFirst()) {
        fav_name = cursor.getString(getAllDataInCurrentLocale.getColumnIndex(FAVOURITES_COL_NAME));
        manageNonFavouritesListView();

    }
    cursor.close();
    getAllDataInCurrentLocale = dbSqlite.getDataInCurrentLocaleById(this, fav_id);
    if (getAllDataInCurrentLocale.moveToFirst()) {
        fav_name = getAllDataInCurrentLocale.getString(getAllDataInCurrentLocale.getColumnIndex(FAVOURITES_COL_NAME));
        manageNonFavouritesListView();

    }
    getAllDataInCurrentLocale.close();
listView1 = this.findViewById(R.id.listview1); //<<<<< ADDED note name should match layout so may be different