Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete item by clicking any item in listview
    primarykey
    data
    text
    <p>I made a listview where at the header an edit text field to add list.And it works fine and successfully add item. Then I try to call this listview and set the adapter. Now what I want is when I click the item it should be deleted, but its getting force closed. Here is my code:</p> <pre><code>public class AddDeleteItemActivity extends ListActivity { public ListView listViewCity; public Context ctx; ArrayList list = new ArrayList(); ArrayAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.btnAdd); ctx=this; adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list); OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { EditText edit = (EditText) findViewById(R.id.txtItem); list.add(edit.getText().toString()); edit.setText(""); adapter.notifyDataSetChanged(); } }; listViewCity = ( ListView ) findViewById( R.id.list); listViewCity.setAdapter(adapter); listViewCity.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; a, View v, int position, long id) { Toast.makeText(getApplicationContext(), " " +position , Toast.LENGTH_LONG).show(); SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions(); int itemCount = getListView().getCount(); for(int i=itemCount-1; i &gt;= 0; i--){ if(checkedItemPositions.get(i)){ adapter.remove(list.get(i)); } } adapter.notifyDataSetChanged(); } }); btn.setOnClickListener(listener); setListAdapter(adapter); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload