我想从列表视图上的选定项目中获取ID,并将其放在onItemclickListener上。我不知道如何实现它。任何想法都会有所帮助。
从sqlite获取所有记录并将其显示在列表视图中
public ArrayList<String> getAllGroceries(){
Cursor result;
ArrayList<String> data = new ArrayList<String>();
String sql = "SELECT * FROM "+TABLE_NAME;
SQLiteDatabase sqldb = this.getWritableDatabase();
result = sqldb.rawQuery(sql,null);
while(result.moveToNext()){
data.add(result.getString(1));
}
return data;
}
这是我在数据库助手Get Grocery中的代码
public Groceries getGrocery(int grocery_id) {
Groceries groce = new Groceries();
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {KEY_GROCE_ID, KEY_GROCE_NAME, KEY_GROCE_PRICE, KEY_GROCE_STOCK, KEY_GROCE_TYPE, KEY_GROCE_BB, KEY_GROCE_DESCRIPT};
String selection = KEY_GROCE_ID + " =? ";
String[] selectionArg = {String.valueOf(grocery_id)};
Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArg, null, null, null);
if (null != cursor) {
cursor.moveToFirst();
groce.setGrocery_id(cursor.getInt(0));
groce.setGroceryname(cursor.getString(1));
groce.setGroceryprice(cursor.getInt(2));
groce.setStock(cursor.getInt(3));
groce.setType(cursor.getString(4));
groce.setBestbefore(cursor.getString(5));
groce.setDescription(cursor.getString(6));
}
db.close();
return groce;
}
这是我在Grocery_list Listview中的代码,请告诉我该怎么做?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grocery_list);
lvGorc = (ListView) findViewById(R.id.lvGroc);
dbhelper = new DatabaseHelper(Grocery_list.this);
final ArrayList<String> aList = dbhelper.getAllGroceries();
final ArrayAdapter<String> La = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, aList);
lvGorc.setAdapter(La);
final Groceries groceries = new Groceries();
lvGorc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> adapterView, View view, int i, long l) {
AlertDialog.Builder adb = new AlertDialog.Builder(Grocery_list.this);
adb.setTitle("Option");
adb.setMessage("What do you want to do?");
adb.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
adb.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
adb.show();
}
}
}
谢谢
答案 0 :(得分:1)
仅,如果字符串(通过 getAllGroceries 方法提取的字符串)是唯一的,那么您可以使用与 > getGrocery 方法来查找(选择)ID。
但是,更正确的方法是使用 ArrayAdapter<Groceries>
并使用 ArrayList<Groceries>
作为ArrayAdapter的源。然后,您可以将Groceries current_groceries = (groceries) La.getItem(i);
与int current_id.getGrocery_id();
一起使用
您可能希望使用其他方法来获得 ArrayList ::-
public ArrayList<Groceries> getAllGroceriesAsArrayListOfGroceries() {
ArrayList<Groceries> rv = new ArrayList();
SQLiteDatabase db = this.getWritableDatabase(); //<<<<<<<<<< NOTE getReadable will most likely getWritable anyway
String[]columns = {KEY_GROCE_ID,KEY_GROCE_NAME,KEY_GROCE_PRICE,KEY_GROCE_STOCK,KEY_GROCE_TYPE,KEY_GROCE_BB,KEY_GROCE_DESCRIPT};
Cursor cursor = db.query(TABLE_NAME,columns,null,null,null,null,null);
// if(null !=cursor) { //<<<<<<<<<< NEVER CHECK CURSOR FOR NULL IT WILL NOT BE NULL
while (cursor.moveToNext) { // Cursor move???? methods will return false if unable to do the move
Groceries groce = new Groceries();
groce.setGrocery_id(cursor.getInt(0));
groce.setGroceryname(cursor.getString(1));
groce.setGroceryprice(cursor.getInt(2));
groce.setStock(cursor.getInt(3));
groce.setType(cursor.getString(4));
groce.setBestbefore(cursor.getString(5));
groce.setDescription(cursor.getString(6));
rv.add(groce);
}
db.close();
return rv;
}
getGrocery方法而不是检查行是否存在,而是检查返回的Cursor是否为null。在没有行的情况下,这(以及未检查 moveToFirst 的结果)将导致索引越界错误。从SQLiteDatabase方法返回的Cursor不会为null,如果没有行,则Cursor将是一个有效的Cursor,只是为空(例如Cursor getCount()方法将返回0或Cursor move ??????方法将返回false) )。
这样,检查游标是否为null充其量是没有用的,但可能是有害的。以下是建议的 getGrocery 方法:-
public Groceries getGrocery(int grocery_id)
{
Groceries groce = new Groceries();
SQLiteDatabase db = this.getReadableDatabase();
String[]columns = {KEY_GROCE_ID,KEY_GROCE_NAME,KEY_GROCE_PRICE,KEY_GROCE_STOCK,KEY_GROCE_TYPE,KEY_GROCE_BB,KEY_GROCE_DESCRIPT};
String selection = KEY_GROCE_ID+ " =? ";
String[]selectionArg = {String.valueOf(grocery_id)};
Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArg,null,null,null);
if(cursor.moveToFirst) { //<<<<<<<<<< checks the returned value from the move
groce.setGrocery_id(cursor.getInt(0));
groce.setGroceryname(cursor.getString(1));
groce.setGroceryprice(cursor.getInt(2));
groce.setStock(cursor.getInt(3));
groce.setType(cursor.getString(4));
groce.setBestbefore(cursor.getString(5));
groce.setDescription(cursor.getString(6));
}
db.close();
return groce;
}
Groceries.java
注意将id用作长整数(因为id可能是64位带符号整数)
public class Groceries {
private long Grocery_id;
private String Grocery_name;
private Float Grocery_price;
private int Grocery_stock;
private String Groccery_type;
private String Grocery_bb;
private String Grocery_description;
public Groceries() {
this.Grocery_id = 0;
this.Grocery_name = "";
this.Grocery_price = 0.0f;
this.Grocery_stock = 0;
this.Groccery_type = "NOTHING";
this.Grocery_bb = "NOTHING";
this.Grocery_description = "";
}
public long getGrocery_id() {
return Grocery_id;
}
public void setGrocery_id(long grocery_id) {
Grocery_id = grocery_id;
}
public String getGrocery_name() {
return Grocery_name;
}
public void setGrocery_name(String grocery_name) {
Grocery_name = grocery_name;
}
public Float getGrocery_price() {
return Grocery_price;
}
public void setGrocery_price(Float grocery_price) {
Grocery_price = grocery_price;
}
public int getGrocery_stock() {
return Grocery_stock;
}
public void setGrocery_stock(int grocery_stock) {
Grocery_stock = grocery_stock;
}
public String getGroccery_type() {
return Groccery_type;
}
public void setGroccery_type(String groccery_type) {
Groccery_type = groccery_type;
}
public String getGrocery_bb() {
return Grocery_bb;
}
public void setGrocery_bb(String grocery_bb) {
Grocery_bb = grocery_bb;
}
public String getGrocery_description() {
return Grocery_description;
}
public void setGrocery_description(String grocery_description) {
Grocery_description = grocery_description;
}
}
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "grocer.db";
public static final int DBVERSION = 1;
public static final String TB_GROCE = "grocery_table";
public static final String KEY_GROCE_ID = BaseColumns._ID; //<<<<<<<<<< used as CursorAdapters expect _id column
public static final String KEY_GROCE_NAME = "grocery_name";
public static final String KEY_GROCE_PRICE = "grocery_price";
public static final String KEY_GROCE_STOCK = "grocery_stock";
public static final String KEY_GROCE_TYPE = "grocery_type";
public static final String KEY_GROCE_BB = "grocery_bb";
public static final String KEY_GROCE_DESCRIPT = "grocery_description";
static final String crt_groce_sql = "CREATE TABLE IF NOT EXISTS " + TB_GROCE + "(" +
KEY_GROCE_ID + " INTEGER PRIMARY KEY, " +
KEY_GROCE_NAME + " TEXT, " +
KEY_GROCE_PRICE + " REAL DEFAULT 0.0, " +
KEY_GROCE_STOCK + " INTEGER, " +
KEY_GROCE_TYPE + " TEXT, " +
KEY_GROCE_BB + " TEXT, " +
KEY_GROCE_DESCRIPT + " TEXT " +
")";
public DatabaseHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
this.getWritableDatabase(); // Force open/create when the helper is instantiated
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(crt_groce_sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long add(String name, float price, int stock, String type, String bb, String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(KEY_GROCE_NAME,name);
cv.put(KEY_GROCE_PRICE,price);
cv.put(KEY_GROCE_STOCK,stock);
cv.put(KEY_GROCE_TYPE,type);
cv.put(KEY_GROCE_BB,bb);
cv.put(KEY_GROCE_DESCRIPT,description);
return db.insert(TB_GROCE,null,cv);
}
public Groceries getGroceryById(long id) {
Groceries rv = new Groceries();
SQLiteDatabase db = this.getWritableDatabase();
Cursor csr = db.query(TB_GROCE,null,KEY_GROCE_ID + "=?",new String[]{String.valueOf(id)},null,null,null);
if (csr.moveToFirst()) {
rv = getGroceriesFromCsrRow(csr);
}
csr.close();
return rv;
}
public Cursor getGroceriesAsCursor() {
SQLiteDatabase db = this.getWritableDatabase();
return db.query(TB_GROCE,null,null,null,null,null,null);
}
public ArrayList<Groceries> getGroceriesAsArrayListOfGroceries() {
ArrayList<Groceries> rv = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor csr = db.query(TB_GROCE,null,null,null,null,null,null);
while (csr.moveToNext()) {
rv.add(getGroceriesFromCsrRow(csr));
}
return rv;
}
private Groceries getGroceriesFromCsrRow(Cursor csr) {
Groceries rv = new Groceries();
if (csr.isBeforeFirst() || csr.isAfterLast()) return rv;
rv.setGrocery_id(csr.getLong(csr.getColumnIndex(KEY_GROCE_ID)));
rv.setGrocery_name(csr.getString(csr.getColumnIndex(KEY_GROCE_NAME)));
rv.setGrocery_price(csr.getFloat(csr.getColumnIndex(KEY_GROCE_PRICE)));
rv.setGrocery_stock(csr.getInt(csr.getColumnIndex(KEY_GROCE_STOCK)));
rv.setGroccery_type(csr.getString(csr.getColumnIndex(KEY_GROCE_TYPE)));
rv.setGrocery_bb(csr.getString(csr.getColumnIndex(KEY_GROCE_BB)));
rv.setGrocery_description(csr.getString(csr.getColumnIndex(KEY_GROCE_DESCRIPT)));
return rv;
}
}
activity_main.xml
非常基本的1 TextView(在单击项目时(在任一ListView中)显示消息)
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grocery List"
/>
<ListView
android:id="@+id/lvGroc"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp">
</ListView>
<ListView
android:id="@+id/lvGrocAlt"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp">
</ListView>
Grocery_list.java
public class Grocery_list extends AppCompatActivity {
DatabaseHelper mDBHlpr;
ListView lvGrocCursor, lvGrocArrayList;
TextView mMessage;
Cursor grocListCursor;
ArrayList grocListArrayList;
SimpleCursorAdapter mSCA;
ArrayAdapter<Groceries> mAA;
static final String ARRAYLIST = "ARRAYLIST";
static final String CURSORLIST = "CURSORLIST";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDBHlpr = new DatabaseHelper(this);
lvGrocArrayList = this.findViewById(R.id.lvGroc);
lvGrocCursor = this.findViewById(R.id.lvGrocAlt);
mMessage = this.findViewById(R.id.message);
mDBHlpr.getWritableDatabase().delete(DatabaseHelper.TB_GROCE,null,null); // delete all rows so they don't keep on getting added
//Add Some Data
mDBHlpr.add("Baked Beans",0.75f,100,"Tinned Foods","?","Guaranteed to make you ....");
mDBHlpr.add("Milk",2.15f,30,"Dairy","X","Fresh Daily");
mDBHlpr.add("Bananas",3.99f,50,"Fruit and Veg","Z","Loads of potassium");
mDBHlpr.add("Bread",1.37f,20,"Baked Goods","A","Sliced");
refreshGrocArrayList();
refreshGrocCursorList();
}
//!!NOTE!! will display Grocies object reference (as per inherited toString ()) but click will show the correct name and id
private void refreshGrocArrayList() {
grocListArrayList = mDBHlpr.getGroceriesAsArrayListOfGroceries();
if (mAA == null) {
mAA = new ArrayAdapter<Groceries>(this,android.R.layout.simple_list_item_1,grocListArrayList);
lvGrocArrayList.setAdapter(mAA);
lvGrocArrayList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Groceries this_groc = mAA.getItem(i);
setMessage(this_groc,ARRAYLIST);
}
});
} else {
mAA.notifyDataSetChanged();
}
}
//!!NOTE!! will display details
private void refreshGrocCursorList() {
grocListCursor = mDBHlpr.getGroceriesAsCursor();
if (mSCA == null) {
mSCA = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_2,
grocListCursor,
new String[]{DatabaseHelper.KEY_GROCE_NAME,DatabaseHelper.KEY_GROCE_ID},
new int[]{android.R.id.text1,android.R.id.text2},
0
);
lvGrocCursor.setAdapter(mSCA);
lvGrocCursor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Groceries this_groc = mDBHlpr.getGroceryById(l);
setMessage(this_groc,CURSORLIST);
}
});
} else {
mSCA.swapCursor(grocListCursor);
}
}
public void setMessage(Groceries g, String type) {
String msgtxt = "You clicked " +
type +
"for " + g.getGrocery_name() +
". ID is " + String.valueOf(g.getGrocery_id());
mMessage.setText(msgtxt);
}
}
上面的解决方案不是理想的,因为它不显示ArrayList中的数据,而是显示对杂货对象的引用。为了克服这个问题( *使用未经检查或不安全的操作* 警告(由于ArrayList转换为List)),应该使用自定义适配器。
这是一个简单的Custom Adapater的代码 GroceriesListCustomAdapter.java
public class GroceriesListCustomAdapter extends ArrayAdapter<Groceries> {
private Context mContext;
private ArrayList<Groceries> mGroceriesList;
public GroceriesListCustomAdapter(Context context, ArrayList<Groceries> groceriesList) {
super(context, android.R.layout.simple_list_item_2,groceriesList);
mContext = context;
mGroceriesList = groceriesList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater li = ((Activity) mContext).getLayoutInflater();
view = li.inflate(android.R.layout.simple_list_item_2, parent, false);
}
TextView studentName = view.findViewById(android.R.id.text1);
TextView studentId = view.findViewById(android.R.id.text2);
Groceries current_groceries = mGroceriesList.get(position);
studentName.setText(current_groceries.getGrocery_name());
studentId.setText(String.valueOf(current_groceries.getGrocery_id()));
return view;
}
}
要使用上述 Grocery_list.java ,需要按照以下说明进行更改:-
public class Grocery_list extends AppCompatActivity {
DatabaseHelper mDBHlpr;
ListView lvGrocCursor, lvGrocArrayList;
TextView mMessage;
Cursor grocListCursor;
ArrayList grocListArrayList;
SimpleCursorAdapter mSCA;
// ArrayAdapter<Groceries> mAA; <<<<<<<<<< REMOVED
GroceriesListCustomAdapter mGLCA; //<<<<<<<<<< ADDED
static final String ARRAYLIST = "ARRAYLIST";
static final String CURSORLIST = "CURSORLIST";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDBHlpr = new DatabaseHelper(this);
lvGrocArrayList = this.findViewById(R.id.lvGroc);
lvGrocCursor = this.findViewById(R.id.lvGrocAlt);
grocListArrayList = new ArrayList();
mMessage = this.findViewById(R.id.message);
mDBHlpr.getWritableDatabase().delete(DatabaseHelper.TB_GROCE,null,null); // delete all rows so they don't keep on getting added
//Add Some Data
mDBHlpr.add("Baked Beans",0.75f,100,"Tinned Foods","?","Guaranteed to make you ....");
mDBHlpr.add("Milk",2.15f,30,"Dairy","X","Fresh Daily");
mDBHlpr.add("Bananas",3.99f,50,"Fruit and Veg","Z","Loads of potassium");
mDBHlpr.add("Bread",1.37f,20,"Baked Goods","A","Sliced");
//refreshGrocArrayList(); //<<<<<<<<<< REMOVED
refreshCustomGrocArrayList(); //<<<<<<<<<< USES CUSTOM ADAPTER as per GroceriesListCustomAdapter
refreshGrocCursorList();
}
private void refreshCustomGrocArrayList() {
grocListArrayList = mDBHlpr.getGroceriesAsArrayListOfGroceries();
if (mGLCA == null) {
mGLCA = new GroceriesListCustomAdapter(this,grocListArrayList);
lvGrocArrayList.setAdapter(mGLCA);
lvGrocArrayList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Groceries this_groc = (Groceries) mGLCA.getItem(i);
setMessage(this_groc,ARRAYLIST);
}
});
}
}
/* <<<<<<<<<< REMOVED
//!!NOTE!! will display Grocies object reference (as per inherited toString ()) but click will show the correct name and id
private void refreshGrocArrayList() {
grocListArrayList = mDBHlpr.getGroceriesAsArrayListOfGroceries();
if (mAA == null) {
mAA = new ArrayAdapter<Groceries>(this,android.R.layout.simple_list_item_1,grocListArrayList);
lvGrocArrayList.setAdapter(mAA);
lvGrocArrayList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Groceries this_groc = mAA.getItem(i);
setMessage(this_groc,ARRAYLIST);
}
});
} else {
mAA.notifyDataSetChanged();
}
}
*/
//!!NOTE!! will display details
private void refreshGrocCursorList() {
grocListCursor = mDBHlpr.getGroceriesAsCursor();
if (mSCA == null) {
mSCA = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_2,
grocListCursor,
new String[]{DatabaseHelper.KEY_GROCE_NAME,DatabaseHelper.KEY_GROCE_ID},
new int[]{android.R.id.text1,android.R.id.text2},
0
);
lvGrocCursor.setAdapter(mSCA);
lvGrocCursor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Groceries this_groc = mDBHlpr.getGroceryById(l);
setMessage(this_groc,CURSORLIST);
}
});
} else {
mSCA.swapCursor(grocListCursor);
}
}
public void setMessage(Groceries g, String type) {
String msgtxt = "You clicked " +
type +
"for " + g.getGrocery_name() +
". ID is " + String.valueOf(g.getGrocery_id());
mMessage.setText(msgtxt);
}
}