-1

I'm using predefined listview layout to show all the native messaging sms into my application.Code i defined in onCreate method:

lvInb = (ListView) findViewById(R.id.lvInb);
    lvInb.setOnCreateContextMenuListener(this);
    data = fetchInbox();
    if(data!=null)
    {
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1 , data);
        lvInb.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lvInb.setAdapter(adapter);
    }

for fetchInbox() I've created another method:

    public ArrayList<String> fetchInbox()
{
     ArrayList<String> sms = new ArrayList<String>();
     Uri uriSms = Uri.parse("content://sms/inbox");
     Cursor cr = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null);
     cr.moveToFirst();
        while  (cr.moveToNext())
        {
             sms.add(cr.getString(1)+"\n"+cr.getString(3)+"\n");
        }
        cr.close();
        return sms;
    }

code to delete row item in listview is:

private void openDelete() {
     removeListViewItem();

}
public void removeListViewItem() {
    if(intListViewItemPosition != -1){
        lvInb.removeViewAt(intListViewItemPosition);
        adapter.notifyDataSetInvalidated();
        intListViewItemPosition = -1;
    }else{
        Toast.makeText(this, "No item selected", Toast.LENGTH_SHORT).show();

    }
}

code in activity:

ListView lvInb;
ArrayAdapter<String> adapter;
int intListViewItemPosition = -1;
ArrayList<String> data;

Outside onCreate method:

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, view, menuInfo);
    CreateMenu (menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    MenuChoice(item);
    return true;
}


private void CreateMenu(Menu menu)
{
    MenuItem mnu1 = menu.add(0,0,0,"Delete");
    {
        mnu1.setIcon(R.drawable.ic_launcher);
    }}
private boolean MenuChoice(MenuItem item)
{
    switch (item.getItemId()) {
    case 0:
        removeListViewItem();
        break;
}
    return false;
}
public void removeListViewItem() {
    intListViewItemPosition = lvInb.getSelectedItemPosition();
    if(intListViewItemPosition != -1){
        //lvInb.removeViewAt(intListViewItemPosition);
        data.remove(intListViewItemPosition);
        adapter.notifyDataSetInvalidated();
        intListViewItemPosition = -1;
    }else{
        Toast.makeText(this, "No item selected", Toast.LENGTH_SHORT).show();

    }
}

delete option i've provided in menu options. but when i select a row item and click on delete option it shows No item selected

Android beginner
  • 245
  • 2
  • 6
  • 22

2 Answers2

0

Hey in where you are assigning the selected Item position value into "intListViewItemPosition"

If not used please implement this in your listview OnItemClickListener,

intListViewItemPosition= listView.getSelectedItemPosition();

You are trying to remove the selected Item from the listview right ?? for that you have clicked your listview item from the lisview, so you need to implement onClickListener for your listview

your code should be like this,

 your_listView_name.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
           intListViewItemPosition= listView.getSelectedItemPosition();
        }
    });

thats it..

Karthick pop
  • 616
  • 3
  • 16
0

first thing call fetchIndex only once:

declare an ArrayList of string in activity:

ArrayList<String> data;
ArrayAdapter<String> adapter

and in onCreate:

lvInb = (ListView) findViewById(R.id.lvInb);
    lvInb.setOnCreateContextMenuListener(this);
    data= fetchInbox();
    if(data!=null)
    {
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1 , data);
        lvInb.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lvInb.setAdapter(adapter);
    }

now change change removeListViewItem to:

public void removeListViewItem() {
    intListViewItemPosition= lvInb.getSelectedItemPosition();
    if(intListViewItemPosition != -1){
        //lvInb.removeViewAt(intListViewItemPosition);
        data.remove(intListViewItemPosition);
        adapter.notifyDataSetInvalidated();
        intListViewItemPosition = -1;
    }else{
        Toast.makeText(this, "No item selected", Toast.LENGTH_SHORT).show();

    }
}

also have class level variable for adapter so that you can access it in removeListViewItem.

ArrayAdapter<String> adapter;

and in onCreate just say:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1 , data);

If you declare it again in onCreate the class level one would be null and you might get null pointer when your removeListViewItem will get called.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44