ListView Click event - android

I have a List of Items which I retrieved from my Sqlite DB...
I want to set a Click event for each item. How I can customize this event based on the Item clicked????
Be descriptive... I am a beginner.
This is the method that I used to fill data in my List:
private void fillData() {
db = new DBAdapter(this);
db.open();
ArrayList db_results = new ArrayList();
//All Category
//Cursor cursor = db.getAllTitles();
//Single Category
Cursor cursor = db.getTitle(1);
if (cursor.moveToFirst())
{
do {
db_results.add(cursor.getString(4));
} while (cursor.moveToNext());
}
cursor.close();
this.list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, db_results));
}

Call setOnItemClickListener() on the ListView. The AdapterView.OnItemClickListener listener you provide will be given the position (0-based index) and ID (if you were using a CursorAdapter, as you should be, rather than converting the Cursor into an ArrayList), so you will know which item was clicked upon.

Related

How to pass the data in the database to a spinner

I'm creating a method to read all the information in the database and view it through a spinner here is the code i tried for this function
public Spinner loadArtist(){
SQLiteDatabase DB = getReadableDatabase();
String[] projection = {
ArtistMaster.Artist.ARTIST_NAME};
Cursor cursor = DB.query(
ArtistMaster.Artist.TABLE_ARTIST,
projection,
null,
null,
null,
null,
null);
Spinner itemIds = new ArrayList<>();
while(cursor.moveToNext()) {
long itemId = cursor.getLong(
cursor.getColumnIndex(ArtistMaster.Artist.ARTIST_NAME));
itemIds.setAdapter(itemId);
}
cursor.close();
return itemIds;
}
but it gives me an error in this line Spinner itemIds = new ArrayList<>();
Should i declare it as a list instead of spinner
itemIds should be defined as an ArrayList<Long>. A Spinner is a UI element and an ArrayList is a data structure. You will most likely need to map the data to your UI using an adapter of some sort, eg. an ArrayAdapter:
Spinner spinner = ... // findViewById, new Spinner() etc.
ArrayList<Long> itemIds = new ArrayList<>();
//... fill array with artist IDs
spinner.setAdapter(
new ArrayAdapter(
this, // Context, Activity etc.,
android.R.layout.simple_list_item_1, // Spinner TextView item resource ID
itemIds // Data set.
));
By default, ArrayAdapter will call Object#toString() on each data object in the collection.
I'd suggest that it would be easier if you used a Cursor Adpater as they are designed to be used with a Cursor and there is no need to generate arrays.
SimpleCursorAdapter being that, a simple but still pretty flexible adapter for use with Cursors.
The only issue is that a Cursor Adapter requires a column name specifically _id (BaseColumns._ID resolves to this (as used below)).
First have the following member variables (obviously names can be what you wish)
:-
Cursor mCursor;
SimpleCursorAdapter mAdapter;
Spinner spinner;
SQLiteDatabase db;
In the onCreate Method of the activity have
:-
spinner = this.findViewById(R.id.?????); //
db = ???????? (as per your existing code)
manageSpinner();
Have a method
:-
private void manageSpinner() {
mCursor = db.query(
ArtistMaster.Artist.ARTIST_NAME,
new String[]{"*","rowid AS " + BaseColumns._ID}, //<<<<<<<< adds _ID column (unless the table is a WITHOUT ROWID table, pretty unlikely)
null,null,null,null,null
);
if (mAdapter == null) {
mAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
mCursor,
new String[]{"the_column"}, // column(s) from which to extract data
new int[]{android.R.id.text1}, // layout views into which the data is placed
0
);
spinner.setAdapter(mAdapter);
// You want want to do something when an Item in the spinner is clicked (this does nothing as it is)
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//........... do your stuff here
// notes
// id will be the id of the row
// cursor will be positioned, so you can access data from the cursor if needed
}
});
} else {
mAdapter.swapCursor(mCursor);
}
}
Override the activity's onResume (refresh the spinner when returning to activity as underlying data may have changed) and onDestroy (to close the Cursor) methods using
:-
#Override
protected void onDestroy() {
super.onDestroy();
mCursor.close();
}
#Override
protected void onResume() {
super.onResume();
manageSpinner();
}

Retrieve values from database and launch a new activity when listview item is clicked

I created a listview using an adapter. The list view contains elements retrieved from a SQLite database using a distinct query.
When the listview item is clicked, it should retrieve all items which have that value used to do the distinct query.
My query looks like this:
#Override
public List<Registro> distinctByCol3(){
List aux = new ArrayList();
Cursor cursor = db.query(true,Registro.TABLE_NAME,new String[]{
BaseColumns._ID,
RegistroColumns.Col1,
RegistroColumns.Col2,
RegistroColumns.Col3,
},null,null,RegistroColumnas.Col3,null,null,null);
if (cursor.moveToFirst())
do {
Registro registro = this.parseCursorToR(cursor);
if (registro != null)
aux.add(registro);
}
while (cursor.moveToNext()) ;
if(!cursor.isClosed()){
cursor.close();
}
return aux;
}
The java class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_registros);
final ListView listView = (ListView) findViewById(R.id.lvRegistros);
DataManager servicio = new DataManagerImpl(this);
List<Registro> listRM = service.getRegistroDistinctedByCol3();
final ArrayAdapter<Registro> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listRM);
listView.setAdapter(adapter);
After click in the element, I need to launch a new activity and show all the elements whit COL3, which is the value I used in my query.
How can I achieved my goal?
If some clarification is needed, let me know. Thanks.

Getting individual text values from items in listView

I have a custom list view over here that displays data from a sqlite database with the help of a simple cursor adapter.
I would like to know how to display a toast showing the title of each entry (e.g. School) when I hold down each entry (using a longClickListener). Thanks.
The method for the simple cursor adapter which is called during onResume:
private void populateListViewFromDatabase() {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.retrieveAllEntriesCursor();
String[] from = {DBAdapter.ENTRY_TITLE, DBAdapter.ENTRY_DESTINATION};
int[] to = {R.id.tvAlarmTitle, R.id.tvAlarmDestination};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,R.layout.custom_alarm_destination_row,c,from,to);
db.close();
lvDestinations.setAdapter(myCursorAdapter);
}

How to Delete row using context menu?

How can I delete a list item and get an updated list using onContextItemSelected?
How can I delete a row using context menu?
Code:
case R.id.Delete_Note:
Database_Notepad db=new Database_Notepad(Create_Task.this);
db.DeleteNote();
break;
My Database:
public void DeleteNote(int rowId) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Table_Notepad, Col_ID + " = ?", new String[] { String.valueOf(rowId) });
db.close();
}
How can I update my listview after the deletion of an item?
This is how I'm setting data to listview:
Database_Notepad db = new Database_Notepad(Create_Task.this); Cursor c = db.Get_All_Note();
String[] from = new String[] {Database_Notepad.Col_File_Name,Database_Notepad.Col_Due_Date};
int[] to = new int[] {R.id.titlename, R.id.duedatetext};
SimpleCursorAdapter madapter = new SimpleCursorAdapter(Create_Task.this, R.layout.file_row, c, from, to);
this.setListAdapter(madapter);
getListView().setOnItemClickListener(this);
registerForContextMenu(getListView());
db.close();
As per your comment you success to delete the row item, but you failed to update your listview,
So whenever some changes made in listview data you should notify your adater by applying notifyDataSetChanged() like this adapter.notifyDataSetChanged() ,it will reflect in your listview if some items get added or deleted.
hope it works!

How do I refresh my ListView after the database changes?

I am trying to manage a simple list in an Android application. The contents of the list are maintained in a SQLite database. When the user selects and holds on a specific row, a context menu appears with a "Delete" option. When they select "Delete", the row is deleted from the database, but the view does not refresh. When I back out of the application and get back in, the appropriate row has been removed. So, I know the row is deleted, it's just the ListView that doesn't refresh.
Here are the relevant bits of code...
In the onCreate method...
SQLiteDatabase db = tasks.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME,
new String[] { _ID, TITLE, DETAILS, },
null, null, null, null, TITLE + " DESC");
startManagingCursor(cursor);
ListView lv = (ListView) findViewById(R.id.list);
lv.setAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor,
new String[] {TITLE},
new int[] { android.R.id.text1}
));
In the onContextItemSelected method...
switch(item.getItemId()) {
case 0:
SQLiteDatabase db = tasks.getWritableDatabase();
ListView lv = (ListView) findViewById(R.id.list);
SimpleCursorAdapter adapter = (SimpleCursorAdapter) lv.getAdapter();
db.delete(TABLE_NAME, "_ID=?", new String[] {adapter.getCursor().getString(0)});
adapter.notifyDataSetChanged(); // Not working, clears screen and doesn't reload
return true;
}
return super.onContextItemSelected(item);
What am I missing?
Thanks!
Get rid of the notifyDataSetChanged() and call requery() on your Cursor. Here is a sample project demonstrating this.

Categories

Resources