按删除按钮android

时间:2015-10-20 16:23:02

标签: android listview

当我按下删除按钮时我想删除我的列表视图项但问题是我应该首先按listview行然后按删除按钮将其删除。我想在按下删除按钮后立即删除项目,而不是先按列表视图,然后按删除按钮 你能在这种情况下帮助我吗?

这是我的代码

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);

            }
        });  
    }
});

}

这是我的listm的xmlfile:

<?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>

<?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>

1 个答案:

答案 0 :(得分:0)

你走错了路。

您可能需要查看此Example.

您已创建自己的适配器,如:

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