0

I have approx 150 items in my custom listview. I am trying to implement the following:

list.setOnItemClickListener(new OnItemClickListener() {

1. I get position of the item

2. Get the data from an ArrayList

3. Add the data to database.

This part is working fine.

But if I want to remove a particular item from the database by clicking on the item I am not able to achieve this part?

Reason because I am not sure if the item is clicked or not? I have this problem mainly when I want to implement search in the listView and add the selected item to database and remove selected item from database.

    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) 
        {

            if (notfromsearch == true)
            {
                String profile_ID = profileList.get(position).get(KEY_ID);
                String profile_DISPLAY_NAME = profileList.get(position).get(KEY_DISPLAY_NAME);
              //add to database now

            }
            elseif (notfromsearch == false)
            {
                String SearchResult_profile_ID = searchProfileResults.get(position).get(KEY_ID);
                String SearchRResult_profile_DISPLAY_NAME = searchProfileResults.get(position).get(KEY_DISPLAY_NAME);
                //add to database now
            }
        }
    }); 

But how to implement remove from database? I am not able to differentiate if the item is clicked before or not to remove from database.

Can somebody help me out with this?

Thanks!

TheDevMan
  • 5,914
  • 12
  • 74
  • 144

2 Answers2

0

On your list view, use

setOnItemClickListener

and you can do what you want next .

Kiloreux
  • 2,220
  • 1
  • 17
  • 24
  • I have setOnitemClick things are working fine if I select the item but I am not sure how to unselect the item if I want to remove the data from database? – TheDevMan Aug 18 '14 at 10:51
  • You question is not clear enough sir , maybe you need to take a look at this question http://stackoverflow.com/questions/18103721/unselect-the-listview-item-onclick-in-android – Kiloreux Aug 18 '14 at 10:54
  • Please read my question again Sir you will understand. I have list of items shown in custom listview. when i select it I would like to add corresponding data to database. (This part is working fine) but if I want unselect the item (remove data from database) how do i implement this part on the same setOnItemclickListener – TheDevMan Aug 18 '14 at 10:57
0

use context menu for deleting items from the database

on long press display delete/edit option and perform further operation as needed.

check here for context menu sample

EDIT

You can also put button/image in the listview to delete item and then implement your delete operation in the click method.

For Checkbox selection

model class

class CustomClass
{
     boolean checked;
     //other fields
}

in your custom adapter class

ArrayList<CustomClass> data = new ArrayList<CustomClass>();

and in getView() method

checkboxItem.setChecked(data.get(position).getChecked());

and don't forget to change the checked boolean value on checkbox change listener..

Community
  • 1
  • 1
Michael Shrestha
  • 2,547
  • 19
  • 30
  • I don't want to implement long press part. – TheDevMan Aug 18 '14 at 11:08
  • hey! Is this the only way to differentiate the add and delete? – TheDevMan Aug 18 '14 at 11:10
  • I did try that out, but I had a strange issue: I added checked box to the custom ListView. The data was showing fine, but the strange issue is that when I check the first row in my list and scroll down I can see another item checked. This is happening everything 7th item on my list. The item is checked automatically. I couldn't solve that issue hence was looking for different solution – TheDevMan Aug 18 '14 at 11:15
  • i did have the same kind of issue and i think i solved that using custom model class with additional boolean field checked which was used for checking/unchecking the checkbox. – Michael Shrestha Aug 18 '14 at 11:17
  • can you help me with a sample project on this? – TheDevMan Aug 18 '14 at 11:21
  • Oh Sorry I don't have sample project at this moment.. i will post if i find free time... – Michael Shrestha Aug 18 '14 at 11:27