我查询我的数据库并返回已排序的数组以填充ListView
。当我点击ListView
时,我可以根据我的数据库获取行ID,但我的上下文菜单无法访问同一个数组,有关如何修复此问题的任何指导?我在网上找到的大多数示例都无济于事,因为我的数据库的行ID与ListView
id不匹配,因为正在进行排序。
这将调用数据库并显示信息:
public class SQLView extends ListActivity {
private SimpleAdapter adapter;
ListView drinkList;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Full Screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sqlview);
// Definitions
SharedPreferences someBar;
someBar = getSharedPreferences("BarPreferences", 0);
String originalBar = someBar.getString("currentBar", "My Local Bar");
originalBar = originalBar.replace("'", "''");
// Open the database and retrieve the drinkNames and the ABV values
DBmanager info = new DBmanager(this);
info.open();
customObject data = info.sortData(originalBar);
String[] drinkNames = data.getNames();
String[] drinkScores = data.getScores();
final String[] drinkRowId = data.getRowId();
info.close();
drinkList = (ListView) getListView();
// Create the mapping
String[] from = new String[] { "rowid", "col_1" };
int[] to = new int[] { R.id.item_tvdrinkName, R.id.item_tvAbvValue };
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < drinkNames.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + drinkNames[i]);
map.put("col_1", "" + drinkScores[i]);
fillMaps.add(map);
}
adapter = new SimpleAdapter(this, fillMaps, R.layout.list_item, from,
to);
drinkList.setAdapter(adapter);
// Register the ListView for Context menu
registerForContextMenu(getListView());
drinkList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
Toast.makeText(SQLView.this,
"Row ID of database entry " + drinkRowId[(int) id], //This returns the SQLite Row ID
Toast.LENGTH_SHORT).show();
}
});
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
// ******THIS IS THE CONTEXT MENU IN QUESTION
AdapterContextMenuInfo information = (AdapterContextMenuInfo) item.getMenuInfo();
long index = information.id;
switch (item.getItemId()) {
case R.id.edit:
Toast.makeText(SQLView.this, "Edit " + index, Toast.LENGTH_SHORT).show(); //This returns the Listview index
break;
case R.id.delete:
Toast.makeText(SQLView.this, "Delete "+ index, Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
这是我的数据库助手类(sortData()
是感兴趣的方法,只有这样才能看到它返回数组):
public class DBmanager {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "drink_name";
public static final String KEY_BAR = "bar_name";
public static final String KEY_SCORE = "drink_score";
public static final String KEY_VOL = "volume";
public static final String KEY_PRICE = "price";
public static final String KEY_DRINKABV = "abv";
// Strings for ABV
public static final String KEY_ABV_ID = "_id";
public static final String KEY_NAME_ABV = "drink_name";
public static final String KEY_ABV = "abv_value";
private static final String DATABASE_NAME = "DBmanagerdb";
private static final String DATABASE_TABLE = "drinkTable";
private static final String ABV_TABLE = "abvTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_BAR
+ " TEXT NOT NULL, " + KEY_NAME + " TEXT NOT NULL, "
+ KEY_PRICE + " DOUBLE, "+ KEY_VOL + " DOUBLE, "
+ KEY_DRINKABV + " DOUBLE, " + KEY_SCORE + " DOUBLE);");
db.execSQL("CREATE TABLE " + ABV_TABLE + " (" + KEY_ABV_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME_ABV
+ " TEXT NOT NULL, " + KEY_ABV + " DOUBLE);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + ABV_TABLE);
onCreate(db);
}
}
public DBmanager(Context c) {
ourContext = c;
}
public DBmanager open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public boolean deleteDatabase(Context context) {
return context.deleteDatabase(DATABASE_NAME);
}
public void close() {
ourHelper.close();
}
public long createEntry(String bar, String name,Double price, Double vol, Double abv, Double score) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_BAR, bar);
cv.put(KEY_NAME, name);
cv.put(KEY_PRICE, price);
cv.put(KEY_VOL, vol);
cv.put(KEY_DRINKABV, abv);
cv.put(KEY_SCORE, score);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
//***************************THIS IS THE METHOD OF INTEREST
public customObject sortData(String bar) {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_BAR, KEY_NAME,
KEY_SCORE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_BAR + " ='"
+ bar + "'", null, null, null, KEY_SCORE + " DESC");
int iName = c.getColumnIndex(KEY_NAME);
int iscore = c.getColumnIndex(KEY_SCORE);
int iRow = c.getColumnIndex(KEY_ROWID);
c.moveToLast();
String[] names = new String[c.getCount()];
double[] scores = new double[c.getCount()];
String[] scores1= new String[c.getCount()];
String[] rowid = new String[c.getCount()];
String result = "";
String myScores = "";
String rows="";
int count = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
rowid[count] = c.getString(iRow);
scores[count] = c.getDouble(iscore);
DecimalFormat df = new DecimalFormat("#.##");
scores1[count]= Double.toString(Double.valueOf(df.format(scores[count])));
/* in case i break something later
for (int i =0; i<scores.length;i++){
scores1[i]= Double.toString(Double.valueOf(df.format(scores[i])));
System.out.println(scores1[i]);
}
*/
//scores1[count] = Double.toString(c.getDouble(iscore));
names[count] = c.getString(iName);
//Below is for string formatting
//result = result + names[count] + "\n";
//myScores = myScores + String.format("%.2f", scores[count]) + "\n";
count++;
}
c.close();
//Used to format the array so it's pretty pretty
return new customObject(names, scores1, rowid);
}
public class customObject {
private String[] names; //array2
private String[] scores; //array1
private String[] rowid;
public customObject(String[] names, String[] scores, String[] id){
this.names = names;
this.scores = scores;
this.rowid = id;
}
public String[] getNames() {
return names;
}
public String[] getScores() {
return scores;
}
public String[]getRowId() {
return rowid;
}
//Setters + getters. Etc.
}
public String getName(long l) throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_SCORE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public String getscore(long l) throws SQLException {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_SCORE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String score = c.getString(2);
return score;
}
return null;
}
public void updateEntry(long lRow, String mName, String mscore)
throws SQLException {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_NAME, mName);
cvUpdate.put(KEY_SCORE, mscore);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "=" + lRow,
null);
}
public void deleteEntry(long lRow1) throws SQLException {
// TODO Auto-generated method stub
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lRow1, null);
}
public boolean deleteEverything(Context context) {
// TODO Auto-generated method stub
return context.deleteDatabase(DATABASE_NAME);
}
public String[] viewABV() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ABV_ID, KEY_NAME_ABV, KEY_ABV};
Cursor c = ourDatabase.query(ABV_TABLE, columns, null, null, null, null, null);
int iDrink = c.getColumnIndex(KEY_NAME_ABV);
int iABV = c.getColumnIndex(KEY_ABV);
int precounter = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
precounter++;
}
String[] drinks;
double[] abv;
int count = 0;
// figure out how to preallocate better
drinks = new String[precounter];
abv = new double[precounter];
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
abv[count] = c.getDouble(iABV);
drinks[count] = c.getString(iDrink);
count++;
}
c.close();
//Real similar to the other ones, we just don't format it as a long string.
return drinks;
}
public long createEntryABV(String name, String abv) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
double value = Double.parseDouble(abv);
cv.put(KEY_NAME_ABV, name);
cv.put(KEY_ABV, value);
return ourDatabase.insert(ABV_TABLE, null, cv);
}
public String getabv(String l) throws SQLException {
Cursor c = ourDatabase.rawQuery("SELECT * FROM abvTable WHERE TRIM(drink_name) = '"+l.trim()+"'", null);
if (c != null) {
c.moveToFirst();
String abv = c.getString(c.getColumnIndex("abv_value"));
return abv;
}
return null;
}
}
答案 0 :(得分:1)
您在id
回调中没有onContextMenuItem()
,但您确实拥有构建了上下文菜单的行的position
,您可以使用它来查找ID:
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo information = (AdapterContextMenuInfo) item.getMenuInfo();
int rowPosition = information.position;
HashMap<String, String> rowdata = adapter.getItem(position);
long index = rowData.get("rowid"); // I'm assuming that is the id
// profit...
对于非基于游标的适配器,为行返回的id将只是该行在适配器中的位置。