Need help to delete selected item from listview - android

I want to delete a selected item from the list view;
actually I want to perform this operation from the context menu. Everything is going fine but I'm not able to delete that item.
Please give me some suggestions or examples to remove item from the listview

I have used like this in my code, it can delete multiple items from the list
ListView lv_ArchivePartylist;
ArrayList<Parties> select_archived_party;
lv_ArchivePartylist = (ListView)findViewById(R.id.archive_ListView01);
lv_ArchivePartylist.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
if(view.findViewById(R.id.img_chkbox_archive).getVisibility()==TextView.GONE)
{
view.findViewById(R.id.img_chkbox_archive).setVisibility(TextView.VISIBLE);
Toast.makeText(ctx_archive, "Name="+archived_parties.get(position).getPartyTitle(), Toast.LENGTH_SHORT).show();
select_archived_party.add(archived_parties.get(position));
}
}
});
Then I've declared one button of "Delete" and on it's On ClickListener method, it calls the code from the database(In your case it may be Arraylist or array) to delete the items selected in Arraylist "select_archived_party". Hope it helps :-)

Related

How to disable all elements (also the ones not currently visible in the screen) in a listview?

I have a ListView with a set of elements. When I click one of the I would like to disable all the others. If I click again on the selected item all the other items are enabled again.
I tried the different proposed solution without success. I hope someone can help me.
This is my code:
//action to take when a presentation is selected
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
//disable the other items when one is selected
if(position!=selectedPresentation){
for(int i=0;i<parent.getCount();i++){
if(i!=position)//disable all the items except the select one
parent.getChildAt(i).setEnabled(false);
}
selectedPresentation=position;
}else if(selectedPresentation==position){
//enable again all the items
for(int i=0;i<parent.getCount();i++){
parent.getChildAt(i).setEnabled(true);
}
selectedPresentation=-1;
}
Where selectedPresentation is a global variable storing the selected item. If no item is selected its value is -1.
Thank you for your help!
Make your own subclass of ArrayAdapter that has AreAllItemsEnabled() return false, and define isEnabled(int position) to return false for a given item in your the ones you want to disable.
In your custom ArrayAdapter overide isEnabled method as following
#Override
public boolean isEnabled(int position) {
return false;
}
other option
Don't implement onItemclickListener. This will not give you any update of item click. Only register onClick listener to views.
You may take another object of the List(Arraylist) which is populating elements in listview then on click copy the corresponding position data to new arraylist you made and then notifyDataSet and when you click again you populate listview with original list so it will apear again....Just do this trick it might work for you
parent.getChildeAt() only work for visible Item.
you should change something in adapter.
if make custom adapter then you can do some thing like #ṁᾶƔƏň ツ answer, but if you use default adapter you can change adapter's arraylist that before you pass it to adapter.
put boolean in it (in arraylist) and in on item click true/false it for all item.
I wrote this solution and seems it works fine!
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
//disable the other items when one is selected
Toast.makeText(context, "onClick called",Toast.LENGTH_SHORT).show(); // this will flash up twice
if(position!=selectedPresentation){
selectedPresentation=position;
for(int i=0;i<adapter.getCount();i++){
if(i!=position)//disable all the items except the select one
adapter.isEnabled(i);
}
}else if(selectedPresentation==position){
//enable again all the items
selectedPresentation=-1;
for(int i=0;i<adapter.getCount();i++){
adapter.isEnabled(i);
}
}
And in my adapter I wrote:
public boolean isEnabled(int position) {
if(selectedPresentation==-1 || selectedPresentation==position)
return true;
return false;
Now my concern is how to show the items in the listView as disabled

Highlighting the list item of a list view in android for a specif time

In my android application i'm having a list view and some items in that,now my requirement is when I select a particular item from that list the list item should be highlighted with a red color, and that selection should not be disappear because in my application i should choose an item from the list view and i should click a button(submit).
So in order to know to the user that he has selected an item from the list view it should be highlighted until he clicks submit button.
I have used choice Mode attribute and set the value to single choice and i have changed the color for highlighting everything works fine but i need it not to be disappeared until user clicks the submit button
The variable val consists of some names which i retrieved from database.
Please help me to solve this Thanks in advance.
ArrayAdapter<String> adptr= new ArrayAdapter<String>(this,R.layout.row,R.id.member_name,array);
lv.setAdapter(adptr);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
// TODO Auto-generated method stub
val=lv.getItemAtPosition(pos).toString();
//Toast.makeText(getApplicationContext(), val, 5000).show();
}
});
use OnItemClickListener
ListView lview = getListView();
int pos =0;
lview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// set the background of v here
if (position == pos)
// reset the background color
// and pos = position;
else
get the previous listitem view and reset it's background to original
}
});
Actually I had the same problem sometime back. I was working on non touch andorid ui application for google tv.
The solution is like this.
1) create a custom ArrayAdapter (extending ArrayAdapter)
2) onItemClick get the position of the item.
3) send that position to your adapter by some public method say setCurrentPosition(int pos)
4) in getView() of the adapter check for the position and set the background to red for that view.
Hope this will work as it worked for me.

Getting list row id in list view

If I have a listview being populated with webservices and the elements added in each row have a unique id stored in the array list which utilizes a hashmap to store data,thn what would be the simplest way to obtain the id of the data,that was clicked in the row..
I am using a base adapter on my list,
Any help would be greatly appreciated.
set on item click listener on listview. it will return position of the clicked item in the adapter (equivalent to position in the array list). you can fetch the id using that.
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
Third parameter is Position of Item in the list.
use this method listView.getItemAtPosition(position). It got a position you can use
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// Get item at position like this:
// listView.getItemAtPosition(position));
}
});
if you use the adapter.getItem(position) function you'll be able to get the item stored in the ListView in that specific position.
update: this is of-course in the setOnItemClickListener

Delete an item from the listview and refresh it on a button click in each row

Hi a am struggling with this part. I want to simply delete an item from the listview when the button on that row is clicked.
I have tried
holder.button.setText("End");
holder.button.setTag(position);
holder.button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Integer index = (Integer) v.getTag();
app_details.remove(index.intValue());
notifyDataSetInvalidated();
}
});
But it's behavior is unpredictable I mean when click on the button on a row it delete the another item from the listview.
Any one have some idea?
Thanks
I have faced the same problem & finally get rid out of it. Have to tried
holder.button.setOnItemClickListener Follow the steps to do what you want:
Implement OnItemClickListener in your current activity class.
set button which delete item from list view to setOnClickListener.
Set list view to setOnItemClickListener listener in your anonymous inner class (e.g., in OnClickListener).
notifyDataSetChanged()
Call this Activity once again Using Intent.
here for Example: I take one list view listVw ->
holder.button.setText("End");
holder.button.setTag(position);
holder.button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your needed stuffs...
listView.setOnItemClickListener(this);
}
});
#Override
public void onItemClick(AdapterView arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
//Do your stuff here...
}
You want to uses an instance of the ArrayAdapter since that adapter has the remove method. Otherwise you will need to implement your own remove method and have you custom Adapter extend BaseAdapter. Here is an example of what methods you need to call to remove an item from the list and tell the Adapter to refresh the list.
m_adapter.remove(o);
m_adapter.notifyDataSetChanged();
You want to call notifyDataSetChanged() instead of notifyDataSetInvalidated(). Here is the difference ....
notifyDataSetChanged() - Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
notifyDataSetInvalidated() - Notifies the attached observers that the underlying data is no longer valid or available.

ListView Select Problem

I have a listview in my program. the problem is that. the list is in single choice mode and when i select any item i have 4 buttons on the footer of my page for that item.. alll i want is to know that. how can i keep that item selected and while cliking any button i can get the selected item.
Try this:
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView a, View v, int position, long l) {
arrayPosition=position;
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
where arrayPosition is a static integer variable.
Try this if it helps..
If you are using a ListActivity you can use getSelectedItemId() or getSelectedItemPosition(). Alternatively you can bind your own OnItemClickListener to the ListView object. More information can be found in this tutorial.

Categories

Resources