2015-10-20 137 views
-3

我想刪除我的列表視圖項目,當我按刪除按鈕,但問題是我應該先按列表視圖行然後按刪除按鈕將其刪除。我想在刪除按鈕後立即刪除項目,先不按列表視圖然後按刪除按鈕 你能幫我在這種情況下?通過按刪除按鈕從列表視圖中刪除項目android

,這是我的代碼

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

      } 
     }); 
    } 
}); 

}

,這是我的列表視圖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> 

回答

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

+0

不,我想我有權做這樣:) #SOreadyToHelp @Blackbelt –