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



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

最新更新