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 :-
Related
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 am a beginner at programming, I want to make a delete button on every items list on recyclerView. I got some references from stack overflow, and its work for the deleted item only at the activity (layout), but when i run the activity again the The selected item showed again.
I found some related articles on stackoverflow and make the method to delete from the SQLite. But my app crushed "unfortunetly app has stopped" every time I call the delete function.
I hope someone can help me to figure it out.
here is my databasehelper class
public class DatabaseHelperClass extends SQLiteOpenHelper {
Log cat Database
public static String log = "DatabaseHelper";
//Databse version
public static final int DATABASE_VERSION = 1;
//Database name
public static final String DATABSE_NAME = "dbPig";
//Tables Name
public static final String TABLE_PIGINFO = "tb_pigInfo";
//Common and PigInfo Column Names
public static final String KEY_ID = "id";
public static final String KEY_NAMA = "nama";
public static final String KEY_TANGGAL_PENDAFTARAN = "tanggal_pendaftaran";
//table create statement
//table pig Info
public static final String CREATE_TABLE_PIGINFO = "CREATE TABLE "
+ TABLE_PIGINFO + "(" + KEY_ID + " INTEGER," + KEY_NAMA
+ " TEXT," + KEY_TANGGAL_PENDAFTARAN
+ " TEXT" + ")";
public DatabaseHelperClass(Context context) {
super(context, DATABSE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
//creating requaired table
db.execSQL(CREATE_TABLE_PIGINFO);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PIGINFO);
// create new tables
onCreate(db);
}
public void insertdata(String nama, String tanggal_pendaftaran) {
System.out.print("Tersimpan" + TABLE_PIGINFO);
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_NAMA, nama);
contentValues.put(KEY_TANGGAL_PENDAFTARAN, tanggal_pendaftaran);
db.insert(TABLE_PIGINFO, null, contentValues);
}
public List<PigInfoTable> getdata() {
List<PigInfoTable> data = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from " + TABLE_PIGINFO + " ;", null);
StringBuffer stringBuffer = new StringBuffer();
PigInfoTable pigInfoTable = null;
while (cursor.moveToNext()) {
pigInfoTable = new PigInfoTable();
String nama = cursor.getString(cursor.getColumnIndexOrThrow("nama"));
String tanggal_pendaftaran = cursor.getString(cursor.getColumnIndexOrThrow("tanggal_pendaftaran"));
pigInfoTable.setNama(nama);
pigInfoTable.setTanggal_pendaftaran(tanggal_pendaftaran);
stringBuffer.append(pigInfoTable);
data.add(0, pigInfoTable);
}
for (PigInfoTable mo : data) {
Log.i("Hellomo", "" + mo.getNama());
}
return data;
}
public void delete(int position) {
SQLiteDatabase db = this.getWritableDatabase();
String table = TABLE_PIGINFO;
String whereClause = KEY_ID;
String [] whereArgs = new String[] {String.valueOf(position)};
db.delete (table, whereClause, whereArgs);
}
and here my adapter
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.Myholder> {
DatabaseHelperClass databaseHelper;
List <PigInfoTable> pigInfoTablesArrayList;
public RecycleAdapter(List <PigInfoTable> pigInfoTablesArrayList) {
this.pigInfoTablesArrayList = pigInfoTablesArrayList;
}
class Myholder extends RecyclerView.ViewHolder{
private TextView nama, tanggal_pendaftaran;
private Button delete;
public Myholder(View itemView) {
super(itemView);
nama = (TextView) itemView.findViewById(R.id.nama1);
tanggal_pendaftaran = (TextView) itemView.findViewById(R.id.tanggal1);
delete = (Button) itemView.findViewById(R.id.delete);
}
}
#Override
public Myholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_itempigview,null);
return new Myholder(view);
}
#Override
public void onBindViewHolder(Myholder holder, final int position) {
PigInfoTable pigInfoTable= pigInfoTablesArrayList.get(position);
holder.nama.setText(pigInfoTable.getNama());
holder.tanggal_pendaftaran.setText(pigInfoTable.getTanggal_pendaftaran());
holder.itemView.setClickable(true);
holder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
databaseHelper.delete(position);
pigInfoTablesArrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, pigInfoTablesArrayList.size());
notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return pigInfoTablesArrayList.size();
}
I am trying some other solution but the same error occurred which's
null object references on
Position
error
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.newbreedy.DatabaseHelperClass.delete(int)' on a null object reference
at com.example.newbreedy.RecycleAdapter$1.onClick(RecycleAdapter.java:66)
thank you
com.example.newbreedy.DatabaseHelperClass.delete(int)' on a null object reference
because you have not initialized the reference of
DatabaseHelperClass databaseHelper;
So Add.
databaseHelper =new DatabaseHelperClass (context);
in your recycler adapter
In your code you are sending adapter position so in place of position send KEY_ID.
databaseHelper.delete(position);
pigInfoTablesArrayList.remove(position);
notifyDataSetChanged();
First you have to initialize your DatabaseHelperClass in your adapter like this,
databaseHelper =new DatabaseHelperClass (context);
Than you need to call the delete function and inform the adapter about the removed item like this,
databaseHelper.delete(position);
pigInfoTablesArrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, pigInfoTablesArrayList.size());
I am trying to add a top 10 high scores to my game. The high scores are made of only two things - the score and the difficulty, but so far I don't understand very much how this database works but after several tutorials I have this done
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "highscores";
private static final String TABLE_DETAIL = "scores";
private static final String KEY_ID = "id";
private static final String KEY_TIME = "time";
private static final String KEY_DIFFICULTY = "difficutly";
public DBHandler(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION);}
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_HIGHSCORES_TABLE = "CREATE TABLE " + TABLE_DETAIL + "("
+ KEY_ID + " INTEGER PRIMARY KEY, "
+ KEY_TIME + " TEXT, "
+ KEY_DIFFICULTY + " TEXT)";
db.execSQL(CREATE_HIGHSCORES_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAIL);
onCreate(db);
}
// Adding new score
public void addScore(int score) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TIME, score); // score value
// Inserting Values
db.insert(TABLE_DETAIL, null, values);
db.close();
}
// Getting All Scores
public String[] getAllScores() {
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_DETAIL;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
int i = 0;
String[] data = new String[cursor.getCount()];
while (cursor.moveToNext()) {
data[i] = cursor.getString(1);
i = i++;
}
cursor.close();
db.close();
// return score array
return data;
}
}
And here is the class that I want to control the database from
public class highscores extends Activity {
private ListView scorebox;
#Override
protected void onCreate(Bundle savedInstanceState) {
DBHandler db = new DBHandler(this);
scorebox = (ListView) findViewById(R.id.scorebox);
super.onCreate(savedInstanceState);
setContentView(R.layout.highscores);
Log.d("Insert: ", "Inserting ..");
db.addScore(9000);
Log.d("Reading: ", "Reading all contacts..");
}
}
When I open the page with the high scores in the application - it is blank, how to make it display something, I tried with this command db.addScore(9000); but it doesn't work. Maybe I didn't told the database where to display that data ?
Edit the Highscore Class
public class highscores extends Activity {
private ListView scorebox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscores);
DBHandler db = new DBHandler(this);
scorebox = (ListView) findViewById(R.id.scorebox);
Log.d("Insert: ", "Inserting ..");
db.addScore(9000);
Log.d("Reading: ", "Reading all contacts..");
ArrayList<String>ar=new ArrayList<>();
ar=db.getAllScores();
ArrayAdapter<String>ar=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s);
scorebox.setAdapter(ar);
}
}
Now in Your DBHandler class Edit getAllScores() method like this
public ArrayList getAllScores() {
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_DETAIL;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
ArrayList<String>data=new ArrayList<>();
while (cursor.moveToNext()) {
data.add(cursor.getString(1));
}
cursor.close();
db.close();
// return score array
return data;
}
if you are using emulator you can pull the db file and check if the table is created or not, if you are using a device you can use this command
cd /D D:\Android\SDK\platform-tools // android sdk path
adb -d shell
run-as com.pkg.pkgname
cat /data/data/com.pkg.pkgname/databases/highscores >/sdcard/highscores
it will copy your db file to device sd card. Using Sqlite browser you can open the db file and check the table is created or not.
Also uninstall the app and install it again
Use this to list your score in your listview
public class highscores extends Activity {
private ListView scorebox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscores);
DBHandler db = new DBHandler(this);
scorebox = (ListView) findViewById(R.id.scorebox);
Log.d("Insert: ", "Inserting ..");
db.addScore(9000);
Log.d("Reading: ", "Reading all contacts..");
String []s=db.getAllScores();
ArrayAdapter<String>ar=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s);
scorebox.setAdapter(ar);
}
}
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.