In this Tutorial we will explain Add and delet listview item.show below
main.xml
.............................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/txtItem"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/hintTxtItem"
/>
<Button
android:id="@+id/btnAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/lblBtnAdd"
android:layout_toRightOf="@id/txtItem"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txtItem"
android:text="@string/txtEmpty"
android:gravity="center_horizontal"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txtItem"
android:choiceMode="multipleChoice" >
</ListView>
<Button
android:id="@+id/btnDel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/lblBtnDel" />
</RelativeLayout>
MainActivity.java
...........................................................................................................................................................
package com.android.beginner;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ListActivity {
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btnAdd);
Button btnDel = (Button) findViewById(R.id.btnDel);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, list);
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
EditText edit = (EditText) findViewById(R.id.txtItem);
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
};
OnClickListener listenerDel = new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
adapter.remove(list.get(i));
}
}
adapter.notifyDataSetChanged();
}
};
btn.setOnClickListener(listener);
btnDel.setOnClickListener(listenerDel);
setListAdapter(adapter);
}
0 comments