I have a database app.I want to make each item disappear from main screen after deleting it. It gets deleted after long press but becomes invisible after I exit the app and then again enter. So, how to disappear each item from main screen instantly after a long press?
Here is my code snippet to delete each item----
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
FriendsDbHelper fdb = new FriendsDbHelper(MainActivity.this);
SQLiteDatabase db = fdb.getWritableDatabase();
db.delete(TABLE_NAME,_ID+"=?",new String[]{Long.toString(id)});
return true;
}
});
As you appear to be using a Cursor Adapter (i.e. you are deleting according to the id) then you need to
a) re-build the Cursor and then
b) use either the adapter's swapCursor or notifyDataSetChanged methods.
Note! If you are not using a Cursor Adapter then you will probably
encounter issues if you use the id passed to the
setOnItemLongClickListener method, as this will not necessarily be
the row's id but it will be the same value as position. At first it
would appear to work but could the start deleting the wrong rows or
not delete any rows.
You code would be along the lines of :-
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
FriendsDbHelper fdb = new FriendsDbHelper(MainActivity.this);
SQLiteDatabase db = fdb.getWritableDatabase();
db.delete(TABLE_NAME,_ID+"=?",new String[]{Long.toString(id)});
mycursor = fdb.your_method_to_get_the_cursor(); //<<<< will need changing
your_adapter.swapCursor(mycursor); //<<<< will need changing
return true;
}
});
Here's a working example :-
// Get the ListView according to it's id
imagelist = (ListView) findViewById(R.id.imagelist);
// Get the Cursor
images = imgdbhlpr.getAllImages();
// Instantiate the Cursor Adapter
sca = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
images,
new String[]{ImgDBHelper.DSCR_COL},
new int[]{android.R.id.text1},
0
);
//
imagelist.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
imgdbhlpr.deleteImageRow(id);
images = imgdbhlpr.getAllImages();
sca.swapCursor(images);
return true;
}
});
imagelist.setAdapter(sca);
You need to remove it from the listView. You need to call your list adapter and then call the method notifyDataSetChanged()
Related
I have a problem with receiving _ID value when an item is clicked on listView.
I have this code:
List<SavedSearch> values = mydb.getAllSavedSearches();
ArrayAdapter<SavedSearch> adapter = new ArrayAdapter<SavedSearch>(this,
android.R.layout.simple_list_item_1, values);
//adding it to the list view.
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(adapter);
obj.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
And my problem is that i want in onItemClick to somehow get the _ID value from database of clicked item in listView. int position and long id both are returning just position on the list.
Thanks for help, and I can say that any of previous topics helped me.
If you're using a database I would suggest you do not use an ArrayAdapter and instead use a CursorAdapter. Then simply call the method getItemId() from your CursorAdapter to retrieve the id of an item at a given position. So, do this:
CursorAdapter adapter = new CursorAdapter(Context yourAppContext, Cursor yourDBCursor, false);
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(adapter);
obj.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
adapter.getItemId(position); // This is the _ID value
}
This is just the suggestion. I am not sure this will solve the issue.
Method 1:
Whenever you load the listView just set the id for the row and you can get the id from onItemClikListener method
Method 2:
Add an texView (set visibility gone) and set text as your id and get id using getText() method
Note
Don't use position this is always changing when the listview is reCycling.
I've got a problem with using variable i in methods setViewValue and setOnItemClickListener.
So I need to check value of row in table and then add it to another class.
But for this checking I need to use i in both methods or do it another way.
Can you helpm me with it?
String[] from = new String[] { DB.COLUMN_MON, DB.COLUMN_YEAR };
int[] to = new int[] { R.id.textMonth, R.id.textYear };
scAdapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to);
scAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
for (int i=0; i < 98; i++){
if (cursor.getString(cursor.getColumnIndex(DB.COLUMN_MON)).equals(dataMonths[0]) && cursor.getString(cursor.getColumnIndex(DB.COLUMN_YEAR)).equals(dataYears[i])){
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Main_month.this, month.class);
intent.putExtra("year", dataYears[i]);
intent.putExtra("month", dataMonths[0]);
startActivity(intent);
finish();
}
});
}
}
return false;
}
});
list.setAdapter(scAdapter);
THANK YOU FOR HELP!
I solve my problem this way:
scAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, final Cursor cursor, int columnIndex) {
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Main_month.this, month.class);
intent.putExtra("year", cursor.getString(cursor.getColumnIndex(DB.COLUMN_YEAR)));
intent.putExtra("month", cursor.getString(cursor.getColumnIndex(DB.COLUMN_MON)));
startActivity(intent);
finish();
}
});
return false;
}
});
There are only two ways to use a variable in an onClick method as well as in other methods.
You can declare the variable in the onClick method and pass it as a parameter to the other method you need it in.
You can declare a class level variable (outside both methods) that has proper scope to be accessed by both methods.
And if you're going for the second method, you need a far more descriptive variable name than just i.
I think I see what you are doing. You are adding an item listener to your ListView, except you are doing it over and over and over again. Bad. nhgrif basically answered the question at hand. I will answer by telling you to not loop. In its current context, you cannot save i without declaring a global variable. Only set the setOnItemClickListener once! This is the reason for position variable included as a parameter; so you know which view was clicked. Check the documentation. Hopefully that helps clarify it.
Edit: I should probably clarify, do not loop and set every item listener. Set the lister ONCE and then loop if you have to, within the click listener that is.
In my code there is a listview in which i am doing an operation to delete the listview item. it works but remains displayed in the listview. It is in the second activity.It disappears only after going to the firstactivity and then returns.Please give me the changes that i have to make.
Getclicker.java
public void onItemClick(AdapterView<?> a, View v, int position, final long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(Getclicker.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete the event" + (position+1));
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
eventsData1.delete( id);
adapter.notifyDataSetChanged();
}});
adb.show();
}
delete method in database
public void delete(long id) {
SQLiteDatabase db = this.getReadableDatabase();
db.delete(DATABASE_TABLE, KEY_ROWID + " = ?",
new String[] { String.valueOf(id )});
db.close();
}
As I see, right now you're deleting an item from Database, but you're not deleting it from adapter, that's why deleted item is still in the ListView. When you go to another activity and then go back to the listView, you load changed data in the adapter.
You should delete an item from the adapter too if you want notifyDataSetChanged() to have an effect . . .
Your code should be something like this:
eventsData1.delete( id);
adapter.deleteItem(id);
adapter.notifyDataSetChanged();
add delete() method in your Adapter class . . .
You are deleting on the basis of id i.e Long Type
eventsData1.delete( id);
You should use "position"
eventsData1.delete(position);
adapter.notifyDataSetChanged();
As you mentioned your deleting operation is working fine but the problem is in listView refreshing. Untill and unless you are changing the activity it is not refreshing your list. What you have to really do is that you have to add this piece of code at the end of the line where deleting operation is ending within onItemClick:
yourListView.setAdapter(yourAdapter);
I have a ListView lv which uses a Cursor c from an SQL database to populate it. When an item is selected however, I need to get the ID of the row. How can I do this?
I assume that you are using a SimpleCursorAdapter (or similar), so I would use the OnItemClickListener:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// id references the SQLiteDatabase _id column
}
});
Here's how it worked out for me. In an other class, I have created a method of List type and added the column id & the name of the item in the list from the sqlite database.
Here's the piece of code to retrieve the column id of the selected item.
public List<String> list;
adapt = new MyAdapter(this, R.layout.list_inner_view, list); //custom adapter
listTask.setAdapter(adapt);
listTask.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View v, int position, long l) {
Integer id = list.get(position).getId();
//position is the position of the item in the list.
}
});
Result: the item is at 3rd position in the list. Its database column_id (auto increment) value is 12 because I have deleted previous items. So the result you expect is 12, which is the value of variable id.
If you don't understand this fully, it may be because I haven't posted my entire code. But I'll help you out with your problem.
I have a ListView that I'm populating with a CursorAdapter like this:
SimpleCursorAdapter.ViewBinder viewBinder = new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == cursor.getColumnIndex(MyTableColumns._ID))
{
view.setTag(cursor.getInt(columnIndex));
}
// some other stuff
}
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.my_item_renderer, cursor, from, to);
adapter.setViewBinder(viewBinder);
The aim is to get the ID from the list item clicked:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Object obj = v.getTag();
int myId = Integer.parseInt(obj.toString());
}
However this is always returning null. What am I overlooking? For now I'm just using a hidden text field but I'd like to know what I was doing wrong.
onListItemClick() provides you with a view that is the row in the list. ViewBinder binds values to the TextViews inside this row. Thus the view you call setTag() on is not the same as the view you call getTag() on.
You can either extend SimpleCursorAdapter so you can call setTag() on the correct view, or you can get the first child view of v in onListItemClick() and get the tag of that.
Have you tried this when setting your tag?
view.setTag(new Integer(cursor.getInt(columnIndex)));
Maybe your "if" statement simply returns false and thus no Tag value gets set?