This is a function that i call customer name from database for spinner. When i select the customer name, i also can obtain customer id. How to do it?
public List<String> getAllUsers(String userID) {
List<String> userlist = new ArrayList<>();
String status= "Active";
SQLiteDatabase db = this.getReadableDatabase();
String query = "select " + COLUMN_Customer_NAME + " from customer where " + COLUMN_Customer_USERID + "='" + userID + "'"+" AND " + COLUMN_Customer_STATUS + "='" + status + "'";
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
userlist.add(cursor.getString(0));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return userlist;
}
spinner_province.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
province_id = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
by using that , You get a selected item from spinner.
create your custom spinner and pass array of names and ids to spinner. and by using selecteditemposition get id from array at that position
create a custom adapter for spinner
class customAdapter(context : Context ,items: List<YourUserModel>) :
ArrayAdapter<YourUserModel>(context, resourceID)
with custom adapter on item click u can get the full object with will be holding name,id etc etc..
easier but not recommended way u can add the objects to hashmap or arraylist with the index/id of spinner position . then you can get it from there
Related
I have a problem with deleting items from my database in listview. This is my code from my helper to delete data:
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + DB_TABLE + " WHERE " + COL1 + " = '" + id +"'" + " AND "
+ COL2 + " = '" + name +"'";
db.execSQL(query);
}
It views the data with this code
private void viewData() {
Cursor cursor = bh.viewData();
if (cursor.getCount() == 0) {
Toast.makeText(this, "Nothing to show", Toast.LENGTH_SHORT).show();
} else {
while (cursor.moveToNext()) {
listItem.add(cursor.getString(1));
}
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItem);
lv.setAdapter(adapter);
}
But the problem is I have no idea how to delete it from database
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//It supposed to be deleted
}
});
Thank you in advance
In short you are presneting a list that is a single string extracted from the database.
Your delete is expecting two values. The id and the name (the listed value). Both values are not available from the single string that is listed.
If COL2 were UNIQUE (i.e. the same value would never be used) then you could easily just delete based upon this value but frequently names are not unique in which case it would be impossible to derive the id from the name. You would either have to use an ArrayList of objects (such an object containing both the id and the name) or have another array containing the id's that is in sync with the array of names.
I'd suggest using a CursorAdapter which :-
caters for Cursors and most especially provides the id as the 4th parameter to the onItemClick (for other adapters it is the same as the 3rd parameter i.e. the position).
NOTE the id column MUST be named _id.
Has all the rows in the Cursor used as the source available.
Has the Cursor appropriately positioned when onItemClick (and also onItemLongClick) is called.
Example
The following is an example based upon your code.
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String DB_TABLE = "mytable";
public static final String COL1 = BaseColumns._ID; //<<<<<<<<<IMPORTANT uses the _id column
public static final String COL2 = "mynamecolumn";
public DatabaseHelper(#Nullable Context context) {
super(context, DBNAME, null, DBVERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + DB_TABLE +
"(" +
COL1 + " INTEGER PRIMARY KEY," +
COL2 + " TEXT " +
")"
);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insert(String name) {
ContentValues cv = new ContentValues();
cv.put(COL2,name);
return this.getWritableDatabase().insert(DB_TABLE,null,cv);
}
public int delete(long id) {
SQLiteDatabase db = this.getWritableDatabase();
// ID WILL BE UNIQUE so that's enough to IDentify a row
return db.delete(DB_TABLE,COL1 + "=?", new String[]{String.valueOf(id)});
}
public Cursor viewData() {
SQLiteDatabase db = this.getWritableDatabase();
return db.query(DB_TABLE,null,null,null,null,null,null);
}
}
The above should be similar to what you have BUT note the name _id (obtained via the constant BaseColumns._ID) for COL1.
MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseHelper bh;
Cursor csr;
ListView lv;
SimpleCursorAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = this.findViewById(R.id.myListView);
bh = new DatabaseHelper(this);
addSomeDataIfNone(); //<<<<< Add some testing data
manageListView(); //<<<<< Manage the LIstView
}
private void manageListView() {
csr = bh.viewData();
if (adapter == null) {
adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
csr,
new String[]{DatabaseHelper.COL2},
new int[]{android.R.id.text1},
0
);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
bh.delete(id); //<<<<< uses the 4th parameter
manageListView(); //<<<<< refresh the ListView as the data has changed
}
});
} else {
adapter.swapCursor(csr);
}
}
private void addSomeDataIfNone() {
if(DatabaseUtils.queryNumEntries(bh.getWritableDatabase(),DatabaseHelper.DB_TABLE) > 0) return;
bh.insert("Name 1");
bh.insert("Name 2");
bh.insert("Name 3");
}
#Override
protected void onDestroy() {
csr.close(); // Done with the Cursor so close it
bh.close(); // Done with the Database as this is the Main Activity
super.onDestroy();
}
}
Note how this is also quite similar BUT
uses the SimpleCursorAdapter as the source for the ListView. It iself uses a Cursor as the source for the data.
does not create a new Adapter each time the data is viewed
automatically refreshes the ListView when an item is clicked and thus deleted.
uses the id to delete the clicked item as that is all that is needed to unqiuely identify a row.
Results
When first run :-
When clicking Name 2 :-
can anyone help me with changing the name of the item in listview? I don't have an idea how to do it. I am using SQLite database and this is my update name:
public void doneName(String finishedName,int id, String oldName)
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 + " = '" + finishedName + "' WHERE " + COL1 +
" = '" + id + "'" + " AND " + COL2 + " = '" + oldName + "'";
db.execSQL(query );
}
after that in my activity I have set onItemClickListener where it should change name but it does not, it only shows toast:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = parent.getItemAtPosition(position).toString();
Cursor cursor =th.getItemID(name);
int itemID = -1;
while(cursor.moveToNext())
{
itemID = cursor.getInt(0);
}
if(itemID>-1) {
String item = "Done";
th.doneName(item, selectedID, selectedName);
Toast.makeText(HomePageActivity.this, "You have accomplished this task!", Toast.LENGTH_SHORT).show();
}}
});
I'm afraid you don't update the instance of the relevant adapter item.
If you have a list of Strings that attached with this ListView Adapter you should initialize the String item at the relevant position with the name you want to show. Then call Adapter.notifyDataSetChanged to refresh all UI elements in the ListView with the updated list.
It seems, that you never update the String instance in your Adapter list
You issue is two-fold.
First you are not extracting the updated data from the database (you could assume that the underlying data has been update bit that is risky).
You are not refreshing the Listview with the changes, so it shows the data it knows about.
I'd suggest utilising a CursorAdapter which can simplify matters as Cursor Adapters are designed for use with Cursors and they especially make all data readily available via the adpater as the Cursor is typically positioned at the appropriate row (e.g. when using onItemClick and onItemLongClick)
The following is an example based upon your code that utilises the SimpleCursorAdapter changing the database and the listview when an item in the list is clicked (to demostrate clicking the same row instead of changing the test to Done it reverses the text so it will will flip for each click).
However, to use a Cursor Adapoter you MUST have a column named _id and that column should be a unique integer (should really be a long rather than int) and typically it would be an alias of the rowid (i.e. defined using _id INTEGER PRIMARY KEY (with or without AUTOINCREMENT, best without)). As such there is a constant BaseColumns._ID that contains the value _id.
First the DatabaseHelper (subclass of SQLiteOpenHelper) in this case DatabaseHelper.java :-
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String TABLE_NAME = "mytable";
/*<<<<<<<<<< id column must be _id (BaseColumns._ID) for Cursor Adapters >>>>>>>>>*/
public static final String COLID = BaseColumns._ID;
public static final String COL1 = "mycol1";
public static final String COL2 = "mycol2";
public DatabaseHelper(#Nullable Context context) {
super(context, DBNAME, null, DBVERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" +
COLID + " INTEGER PRIMARY KEY, " +
COL1 + " TEXT, " +
COL2 + " TEXT " +
")"
);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insert(String col1, String col2) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL1,col1);
cv.put(COL2,col2);
return db.insert(TABLE_NAME,null,cv);
}
public int doneName(String finishedName,int id, String oldName) {
/*
Alternative using the update convenience method
Based upon :-
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 + " = '" + finishedName + "' WHERE " + COL1 +
" = '" + id + "'" + " AND " + COL2 + " = '" + oldName + "'";
writes the SQL for you.
protects against SQL Injection
returns the number of rows updated
*/
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL2,finishedName);
return db.update(TABLE_NAME,cv,COLID + "=? AND " + COL2 + "=?",new String[]{String.valueOf(id),oldName});
}
public Cursor getAll() {
SQLiteDatabase db = this.getWritableDatabase();
return db.query(TABLE_NAME,null,null,null,null,null,null);
}
}
Three custom methods insert (inserts a row), getAll (returns a Cursor with all the rows) and your doneName method (rewritten to take advantage the the update convenience method).
You may notice the absence of any methods to convert extracted data into a List/ArrayList/Array of objects. This is because there is no need when using a Cursor adapter.
The activity MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView mListView;
DatabaseHelper th;
Cursor csr;
SimpleCursorAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = this.findViewById(R.id.listview);
th = new DatabaseHelper(this);
addSomeTestData();
manageAdapter();
}
#Override
protected void onDestroy() {
super.onDestroy();
csr.close(); //<<<<<<<<<< Should always close Cursors when done with them
}
private void manageAdapter() {
/* This handles but the initialisation and the refreshing of the Listview */
/* First time it is called it initialises the Adapter and Listview */
/* On subsequent calls it refreshes the ListView */
csr = th.getAll();
if (adapter == null) {
adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_expandable_list_item_2,csr,
new String[]{
DatabaseHelper.COL1,
DatabaseHelper.COL2
},
new int[]{
android.R.id.text1,
android.R.id.text2},
0
);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (th.doneName(
/* reverses string to test multiple updates of the same row */
new StringBuilder(csr.getString(csr.getColumnIndex(DatabaseHelper.COL2))).reverse().toString()
/* "Done" */,
(int )id /* NOTE ID IS PASSED to onItemClick FOR CURSOR ADAPTER */,
csr.getString(csr.getColumnIndex(DatabaseHelper.COL2)) /* NOTE Oldname isn't required as ID will identify the row */
) > 0) {
manageAdapter(); //<<<<<<<<< after updating refresh the Cursor and the ListView
Toast.makeText(view.getContext(),"Updated OK.",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(view.getContext(),"Not Updated!!!",Toast.LENGTH_SHORT).show();
}
}
});
} else {
adapter.swapCursor(csr);
}
}
private void addSomeTestData() {
//Add some data but only if none exists
if (DatabaseUtils.queryNumEntries(th.getWritableDatabase(),DatabaseHelper.TABLE_NAME) > 0) return;
th.insert("TEST1 COL1","TEST1 COL2");
th.insert("TEST2 COL1","TEST2 COL2");
th.insert("TEST3 COL1","TEST3 COL2");
th.insert("TEST4 COL1","TEST4 COL2");
th.insert("TEST5 COL1","TEST5 COL2");
}
}
Note that you can use your own layouts with SimpleCursorAdapter (4th (String[]) and 5th (int[]) are used to specify the column that the data comes from and the id of the respective view into which the data goes to)
Result :-
When initially run you get :-
Clicking a row and you get :-
If you then click all rows you get :-
And so on.
I've created 2 tables within an SQLite database, stok and sales
at DatabaseHelper.java
Create table stok
String tbStok = "CREATE TABLE stok(id_stok INTEGER PRIMARY KEY AUTOINCREMENT, waktu_stok DATETIME, id_sales INTEGER, stok INTEGER, FOREIGN KEY id_sales REFERENCES sales(id_sales)";
Create table sales
String tbSales = "CREATE TABLE sales(id_sales INTEGER PRIMARY KEY AUTOINCREMENT, nama VARCHAR, kodesales VARCHAR, username VARCHAR, password VARCHAR, level INTEGER)";
Create List Data from SQLite
public List<String> getSpinnerSales(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + "sales";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(0));
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
Display on Spinner
private void loadSpinnerSales() {
// database handler
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
// Spinner Drop down elements
List<String> lables = db.getSpinnerSales();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spsales.setAdapter(dataAdapter);
}
Test if spinner selected
spsales.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
String label = parent.getItemAtPosition(position).toString();
Log.d("label:", label);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
I need id_sales to put values and save to other tables SQLite, but the spinner must display the name of sales.
the best way is to use custom adapter then
you can pass List of objects (in your case is"list of sales id and name" )to the adapter not just a List of strings,
then on item selected you will get the selected object
then you can get whatever you need id or name
you can follow this link to make custom adapter
https://abhiandroid.com/ui/custom-spinner-examples.html
I Solve this problems with edited the code
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(0)+cursor.getString(1));
} while (cursor.moveToNext());
}
And at spinner on item selected manipulate with substring
spsupir.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
String label = parent.getItemAtPosition(position).toString();
String ids = String.valueOf(label).substring(0,1);
Log.d("label:", ids);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Code edited
String label = parent.getItemAtPosition(position).toString();
String ids = String.valueOf(label).substring(0,1);
Log.d("label:", ids);
Another Solve Problems
Step 1 Create the model class
public class Sales {
String id_sales,nama;
public Sales(String id_sales, String nama) {
this.id_sales = id_sales;
this.nama = nama;
}
public String getId_sales() {
return id_sales;
}
public void setId_sales(String id_sales) {
this.id_sales = id_sales;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
#Override
public String toString() {
return nama;
}
}
Step 2 Put all data from SQLite table sales
//Data spinner supir
public ArrayList<Sales> getSpinnerSales(){
ArrayList<Sales> salesList = new ArrayList<>();
// Select All Query
String selectQuery = "SELECT * FROM " + "sales";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
salesList.add(new Sales(cursor.getString(0), cursor.getString(1)));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return salesList;
}
Step 3 Generate void to consume data from sqlite aa Activity
private void loadSpinnerSales() {
// database handler
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
ArrayList<Sales> salesList = db.getSpinnerSales();
ArrayAdapter<Sales> adapter = new ArrayAdapter<Sales>(this, android.R.layout.simple_spinner_dropdown_item, salesList);
spsales.setAdapter(adapter);
}
Step 4 Load method at OnCreate
spsales = (Spinner) findViewById(R.id.spSales);
loadSpinnerSales();
spsales.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Sales sales = (Sales) parent.getSelectedItem();
Toast.makeText(getApplicationContext(),sales.getId_sales(),Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I know for sure that the updateFromDatabase() function works, I've used print statements to see that the entries put into mCoordinatesArray are there and not empty strings. However when I restart the app, the fragment never populates the list view with items in the database. I think it has something to do with the Fragment Lifecycle, but I have no idea.
Additionally, when I don't restart the app and run it for the first time the list view runs fine. When I rotate or restart the app, the list view no longer populates.
public class LocalFragment extends Fragment{
private ListView mLocalList;
private ArrayAdapter<String> adapter;
private ArrayList<String> mCoordinatesArray;
private BroadcastReceiver mBroadcastReceiver;
private LocationBaseHelper mDatabase;
private DateFormat dateFormat;
private String dateString;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_local,container,false);
// SQLite Setup
mDatabase = new LocationBaseHelper(getActivity());
mLocalList = (ListView) v.findViewById(R.id.lv_local);
mCoordinatesArray = new ArrayList<>();
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mCoordinatesArray);
if(!mDatabase.size().equals("0")){
updateFromDatabase();
}
mLocalList.setAdapter(adapter);
return v;
}
#Override
public void onResume() {
super.onResume();
if(mBroadcastReceiver == null){
mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
dateFormat = new SimpleDateFormat("MM/dd HH:mm:ss a");
dateString = dateFormat.format(new Date());
String[] data = intent.getStringExtra("coordinates").split(" ");
mDatabase.insertEntry(dateString,data[0],data[1]);
System.out.println(mDatabase.size());
mCoordinatesArray.add(dateString + " " + data[0] + " " + data[1]);
adapter.notifyDataSetChanged();
}
};
}
getActivity().registerReceiver(mBroadcastReceiver, new IntentFilter("location_update"));
}
#Override
public void onDestroy() {
super.onDestroy();
if(mBroadcastReceiver!=null){
getActivity().unregisterReceiver(mBroadcastReceiver);
}
}
// THIS METHOD CAN BE USED TO UPDATE THE ARRAY HOLDING COORDINATES FROM THE LOCAL DATABASE
private void updateFromDatabase(){
//mCoordinatesArray.clear();
mCoordinatesArray = mDatabase.getEntireDatabase();
adapter.notifyDataSetChanged();
}
}
Here's my Helper class, just in case, but I don't think the problem is here.
public class LocationBaseHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;
private static final String DATABASE_NAME = "locationBase.db";
public LocationBaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + LocationTable.NAME + " (" +
LocationTable.Cols.DATE_TIME + " text, " +
LocationTable.Cols.LATITUDE + " text, " +
LocationTable.Cols.LONGITUDE + " text )"
);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insertEntry(String date_time, String latitude, String longitude){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues content = new ContentValues();
content.put(LocationTable.Cols.DATE_TIME,date_time);
content.put(LocationTable.Cols.LATITUDE,latitude);
content.put(LocationTable.Cols.LONGITUDE,longitude);
db.insert(LocationTable.NAME,null,content);
}
public ArrayList<String> getEntireDatabase(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + LocationTable.NAME,null);
cursor.moveToFirst();
ArrayList<String> values = new ArrayList<>();
do{
String value = (String) cursor.getString(cursor.getColumnIndex(LocationTable.Cols.DATE_TIME)) + " " +
(String) cursor.getString(cursor.getColumnIndex(LocationTable.Cols.LATITUDE)) + " " +
(String) cursor.getString(cursor.getColumnIndex(LocationTable.Cols.LONGITUDE));
values.add(0,value);
}while(cursor.moveToNext());
return values;
}
public String size(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM " + LocationTable.NAME,null);
cursor.moveToFirst();
return cursor.getString(0);
}
}
By calling mCoordinatesArray = mDatabase.getEntireDatabase(); you are changing the reference of mCoordinatesArray, and adapter is still holding the old reference, so it does not see any changes.
Instead of creating new instance of mCoordinateArray, you should rather just update values it contains, something like:
mCoordinateArray.clear();
mCoordinateArray.addAll(mDatabase.getData());
adapter.notifyDataSetChange();
That way you are changing the data that is referenced by adapter, instead of creating completely new set of data which the adapter is not aware of.
Try to recreate your ArrayAdapter instead of using .notifyDataSetChanged():
// update Data
mCoordinatesArray = mDatabase.getEntireDatabase();
// create new adapter with new updated array
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mCoordinatesArray);
// set adapter for the listview
mLocalList.setAdapter(adapter);
You can put this method in your fragment and call from activity that attach to fragment.
public void updateList(List<?> result) {
if (multimediaListRent.size()>0) {
multimediaGridView.setAdapter(gridMediaListAdapter);
multimediaListRent.clear();
}
gridMediaListAdapter.notifyDataSetChanged();
}
I would like to use a spinner as a menu to be able to select the name of a workout, upon selection the TextViews (x4) will be populated with the exercises relevant to that name.
Currently the Spinner loads the workout names and have got as far as loading the first workout into each of the TextView's onCreation.
There are no errors when the app is launched, but the other rows don't load into the TextViews when the the new workout name is selected.. One can only assume I have made an error in the logic.
Question: If it is an error in the logic can someone identify it and point me in the direction please?
begin.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_begin);
loadSpinnerData();
refreshTextView();
}
public void loadTextView(){
DataHelper dataHelper = new DataHelper(this);
ArrayList<String> exercises = dataHelper.getExercises();
((TextView) findViewById(R.id.exercise1)).setText(exercises.get(0));
((TextView) findViewById(R.id.exercise2)).setText(exercises.get(1));
((TextView) findViewById(R.id.exercise3)).setText(exercises.get(2));
((TextView) findViewById(R.id.exercise4)).setText(exercises.get(3));
}
public void refreshTextView(){
Spinner databaseR = (Spinner) findViewById(R.id.databaseR);
databaseR.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
loadTextView();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
DataHelper.java
public String Create_ex="CREATE TABLE ExerciserEasy"
+ " (ID INTEGER PRIMARY KEY AUTOINCREMENT, Workout VARCHAR, ExerciseOne VARCHAR,"
+"ExerciseTwo VARCHAR, ExerciseThree VARCHAR, ExerciseFour VARCHAR );"
public ArrayList<String> getExercises(){
ArrayList<String> list = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
db.beginTransaction();
try {
String selectQuery = " Select * from " + Table_ex1;
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
list.add(cursor.getString(2));
list.add(cursor.getString(3));
list.add(cursor.getString(4));
list.add(cursor.getString(5));
Log.d("HIITtimer", ""+cursor.getCount());
}while(cursor.moveToNext());
}
db.setTransactionSuccessful();
}catch (Exception e){
e.printStackTrace();
}finally {
db.endTransaction();
db.close();
}
return list;
}
first you need to pass the selected workout name to loadTextView() so you can pass it to getExercises()
and query the database based on that name, other code looks fine,
1- add parameter workoutName to getExercises(), and use it in WHERE clause
public ArrayList<String> getExercises(String workoutName){
//codes ...
String selectQuery = " Select * from " + Table_ex1 + " WHERE Workout ='" workoutName + "'";
//codes ...
}
Note: you may want to use SQLiteDatabase#query() instead of row query?
2- add parameter workoutName to loadTextView(), and pass it to getExercises():
public void loadTextView(String workoutName){
//code ...
ArrayList<String> exercises = dataHelper.getExercises(workoutName);
//code ...
}
3- pass selected workout from spinner's setOnItemSelectedListener():
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
loadTextView(databaseR.getSelectedItem().toString());
}
now when item is selected, loadTextView() is called, telling the method which workout was selected, and query database to get the record related to that workout name.