Reputation: 172
I want to get the id from selected item on listview and i want to put it on onItemclickListener. I dont have an idea how to implement it. any idea would help.
Getting all the record from sqlite and display it on listview
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;
}
This is my my code in Database helper 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;
}
And this is my code in Grocery_list Listview, please tell me how can I do that?
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();
}
}
}
Thank you
Upvotes: 0
Views: 92
Reputation: 57063
Only if the string (as extracted by the getAllGroceries method) will be unique then you could have a method similar to the getGrocery method to find(select) the id.
However, the more correct way would be to use ArrayAdapter<Groceries>
and use an ArrayList<Groceries>
as the source of the ArrayAdapter. You could then use Groceries current_groceries = (groceries) La.getItem(i);
along with int current_id.getGrocery_id();
You'd likely want an additional method to get the ArrayList along the lines of :-
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;
}
The getGrocery method rather than checking for the existence of rows checks if the returned Cursor is null. This, in the instance that there are no rows, (along with the result of moveToFirst not being checked) would result in an index out of bounds error. A Cursor returned from SQLiteDatabase methods will not be null, if there are no rows the Cursor will be a valid Cursor just empty (e.g. the Cursor getCount() method would return 0 or the Cursor move?????? methods would return false).
As such check a Cursor for null is at best useless but can be harmful. The following is a suggested getGrocery method :-
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
Note uses long for id (as id can potentially be a 64 bit signed integer)
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
Very basic 1 TextView (to display message when item is clicked (in either 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);
}
}
The solution above isn't ideal as it doesn't display the data from the ArrayList but rather it displays the reference to the groceries object. To overcome this (and also the * uses unchecked or unsafe operations* warning (due to the ArrayList being converted to a List)), a Custom Adapter should be used.
Here's the code for a simple 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;
}
}
To use the above Grocery_list.java needs to be changed as per :-
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);
}
}
Upvotes: 1