user3669034 user3669034 - 5 months ago 135
Android Question

remove items from listview by pressing delete button android

I want to delete my listview items when I press delete button but the problem is I should first press listview row then press delete button to remove it. I want to delete items immediately after pressing delete button not first press on listview then press delete button
Can you help me in this situation?

and this is my code

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wholeshop);

ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// getting values from selected ListItem

final View view = v;
final int pos =position;
deleteone= (Button)v.findViewById(R.id.bdeleteone);
deleteone.getTag(position);
deleteone.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
deleteone.setTag(view);
productsList.remove(pos);
ListAdapter adapter = new SimpleAdapter(
wholeShop.this, productsList,
R.layout.listshop, new String[] { "name",
"spinn","price"},
new int[] { R.id.wholename, R.id.wholespinn,R.id.wholeprice });
// updating listview

setListAdapter(adapter);

}
});
}
});


}

and this is my xmlfile for listview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff">

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:layout_marginTop="50dp"
android:descendantFocusability="blocksDescendants"
>
</ListView>


and:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#ffffff"
android:weightSum="100">




<TextView
android:id="@+id/wholename"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="40"
android:paddingLeft="5dip"
android:text="Flower Name"
android:textColor="#000000" />




<TextView
android:id="@+id/wholespinn"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:paddingRight="10dip"
android:text="Flower spinn"
android:textColor="#000000"
android:layout_weight="30"/>

<TextView

android:id="@+id/wholeprice"
android:text="Flower price"
android:layout_gravity="right"
android:paddingRight="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_weight="30"/>

<Button
android:id="@+id/bdeleteone"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Del"
android:layout_marginRight="10dp"
android:layout_weight="30"
android:focusable="false"
/>

</LinearLayout>



Answer

You are going with wrong way.

You may have to look this Example.

You have create Your Own Adapter like:

public class ListAdapter extends ArrayAdapter<String> {  
    customButtonListener customListner;  

    public interface customButtonListener {  
        public void onButtonClickListner(int position,String value);  
    }  

    public void setCustomButtonListner(customButtonListener listener) {  
        this.customListner = listener;  
    }  

    private Context context;  
    private ArrayList<String> data = new ArrayList<String>();  

    public ListAdapter(Context context, ArrayList<String> dataItem) {  
        super(context, R.layout.child_listview, dataItem);  
        this.data = dataItem;  
        this.context = context;  
    }  

    @Override  
    public View getView(final int position, View convertView, ViewGroup parent) {  
        ViewHolder viewHolder;  
        if (convertView == null) {  
            LayoutInflater inflater = LayoutInflater.from(context);  
            convertView = inflater.inflate(R.layout.child_listview, null);  
            viewHolder = new ViewHolder();  
            viewHolder.text = (TextView) convertView  
                    .findViewById(R.id.childTextView);  
            viewHolder.button = (Button) convertView  
                    .findViewById(R.id.childButton);  
            convertView.setTag(viewHolder);  
        } else {  
            viewHolder = (ViewHolder) convertView.getTag();  
        }  
        final String temp = getItem(position);  
        viewHolder.text.setText(temp);  
        viewHolder.button.setOnClickListener(new OnClickListener() {  

            @Override  
            public void onClick(View v) {  
                if (customListner != null) {  
                    customListner.onButtonClickListner(position,temp);  
                }  

            }  
        });  

        return convertView;  
    }  

    public class ViewHolder {  
        TextView text;  
        Button button;  
    }  
}  

enter image description here