user1281948
user1281948

Reputation: 11

Deleting items from ListView with a contextmenu in Android?

I'm trying to make a listmanager application in Android. I made a ListView, and an ArrayList into which I can add items with a button and an EditText. Then I made a context menu with an xml file, which I can delete listitems with, add new ones, edit them etc. The problem is that I don't know how to delete my items within the onContextItemSelected() method of my contextmenu. More precisely, I don't know how to refer to the listitems inside of this method, same with all the other operations, but I hope I can do them when I succeeded with the deleting. (Sorry for my bad english, it's been a while since I properly used it. :))

Here's my main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/linear"
    android:layout_weight="1"
    android:background="#000000"
    android:drawSelectorOnTop="true" />

<LinearLayout 
    android:id="@+id/linear"
    android:layout_alignParentBottom="true"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/entry"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1"/>
    <Button
        android:text="Hozzáad"
        android:id="@+id/bHozzaad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>

Here's my contextmenu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/editItem"
      android:title="edit"/>

<item android:id="@+id/markItem"
      android:title="mark" />

<item android:id="@+id/deleteItem"
      android:title="delete" />

<item android:id="@+id/permItem"
      android:title="permanently delete" />

<item android:id="@+id/copyItem"
      android:title="copy" />

<item android:id="@+id/moveItem"
      android:title="move"/>
</menu>

And here is my main java:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  final ArrayList<String> listaelemek = new ArrayList<String>();

  final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, listaelemek);
  setListAdapter(adapter);
  adapter.add("first item");

  setContentView(R.layout.main);

  ListView lv = getListView();
  lv.setTextFilterEnabled(true);
  registerForContextMenu(lv);   

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

  final Button hozzaad = (Button) findViewById(R.id.bHozzaad);
  hozzaad.setOnClickListener(new View.OnClickListener() 
        {
      public void onClick(View v) 
            {
            EditText entry = (EditText) findViewById(R.id.entry);
            listaelemek.add(entry.getText().toString());
            adapter.notifyDataSetChanged();
            }
        }
  );
}

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}

@Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo menuInfo)    
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
 case R.id.editItem:
//do something 
return true;
 case R.id.markItem:
//do something
 return true;
 case R.id.deleteItem:
//here's my question mark :)
 return true;
 case R.id.permItem:
//do something
return true;
case R.id.copyItem:
//do something
return true;
case R.id.moveItem:
//do something
return true;
default:
return super.onContextItemSelected(item);
}
}

Hope you can help me :)

Upvotes: 1

Views: 16877

Answers (2)

kirti
kirti

Reputation: 183

I wrote above adapter code in my program, when i run program & delete item using context menu at that time force close window come

this is my code

public class ShowContextMenu extends ListActivity {

ArrayAdapter<String> adapter;



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   final String []s=(getResources().getStringArray(R.array.names));

   final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s);

    setListAdapter(adapter);


    registerForContextMenu(getListView());
}

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}






public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

    String[] names = getResources().getStringArray(R.array.names);

    switch(item.getItemId()) {

    case R.id.edit:
        Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.edit) + 
                " context menu option for " + names[(int)info.id],                          
                Toast.LENGTH_SHORT).show();



        return true;

    case R.id.save:
        Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.save) + 
                " context menu option for " + names[(int)info.id],
                Toast.LENGTH_SHORT).show();
        return true;

    case R.id.delete:

// insert code here

adapter.remove(adapter.getItem(info.position));


        Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.delete) + 
                " context menu option for " + names[(int)info.id],
                Toast.LENGTH_SHORT).show();

        return true;

    case R.id.view:

        Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.view) + 
                " context menu option for " + names[(int)info.id],
                Toast.LENGTH_SHORT).show();
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

}

Upvotes: 1

Egor
Egor

Reputation: 40203

This code should do the job:

adapter.remove(adapter.getItem(info.position));

Upvotes: 11

Related Questions