gpt4 book ai didi

java - 在sqlite中使用intent后如何从listview中获取项目名称?

转载 作者:行者123 更新时间:2023-12-01 18:27:24 26 4
gpt4 key购买 nike

我有两个 ListView ,每个 ListView 都处于不同的 Activity 中,我想通过长按它来将项目从第一个 ListView 发送到另一个 ListView ,我使用 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;
}

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

}

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

最佳答案

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

关于java - 在sqlite中使用intent后如何从listview中获取项目名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60210769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com