2014-04-05 67 views
0

當我點擊我應用程序中的編輯按鈕時,我希望我的ListView在每行的右側顯示一個「刪除」按鈕。我將如何去做這件事?單擊按鈕後更改列表視圖?

activity_recording.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:background="@color/darkbluenice" 
    tools:context="edu.berkeley.cs160.lasercats.RecordActivity"> 


    <Button 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="10dp" 
     android:text="Record" 
     android:textSize="12pt" 
     android:id="@+id/recordButton" 
     android:background="@drawable/roundedbutton" 
     android:onClick="recordButtonPressed" 
     android:layout_gravity="left|bottom" /> 

    <Button 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:layout_marginBottom="10dp" 
     android:layout_marginRight="10dp" 
     android:text="Edit" 
     android:textSize="12pt" 
     android:id="@+id/editButton" 
     android:background="@drawable/roundedbutton" 
     android:layout_gravity="right|bottom" /> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="326dp" 
     android:background="@color/closeyoureyes" 
     android:id="@+id/listView" 
     android:layout_gravity="center_horizontal|top" /> 

</FrameLayout> 
+0

添加適配器 – Houcine

+0

的代碼,我沒有一個適配器@Houcine – Opal

+0

看到我的回答如下,我紅粉的教程講解如何創建一個CutomListView @Opal – Houcine

回答

1

您必須編寫自己的Adapter並填充ListView定製Views。 看看this tutorial

1

你可以關注這個tutorial來學習如何在Android中創建一個自定義ListView。

。而要回答你的問題,你的適配器應該有一個布爾變量來檢查,如果在ListView中啓用編輯模式或不喜歡這樣的:

public class CustomAdapter extends BaseAdapter { 

    Context context; 
    boolean isEditModeEnabled = false; 


    protected List<Car> listCars; 
    LayoutInflater inflater; 

    public CustomAdapter (Context context, List<Car> listCars) { 
     this.listCars = listCars; 
     this.inflater = LayoutInflater.from(context); 
     this.context = context; 
    } 

    public int getCount() { 
     return listCars.size(); 
    } 

    public Car getItem(int position) { 
     return listCars.get(position); 
    } 

    public long getItemId(int position) { 
     return listCars.get(position).getDrawableId(); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 

      holder = new ViewHolder(); 
      convertView = this.inflater.inflate(R.layout.layout_list_item, 
        parent, false); 

      holder.txtModel = (TextView) convertView 
        .findViewById(R.id.txt_car_model); 
      holder.txtColor = (TextView) convertView 
        .findViewById(R.id.txt_car_color); 
      holder.txtPrice = (TextView) convertView 
        .findViewById(R.id.txt_car_price); 
      holder.imgCar = (ImageView) convertView.findViewById(R.id.img_car); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     Car car = listCars.get(position); 
     holder.txtModel.setText(car.getModel()); 
     holder.txtColor.setText(car.getColor()); 
     holder.txtPrice.setText(car.getPrice() + " €"); 
     holder.imgCar.setImageResource(car.getDrawableId()); 

     //check if the EditMode is activated or not 
     if(isEditModeEnalbed) { 
      yourDeleteButton.setVisibility(View.VISIBLE) 
     } 
     else 
      yourDeleteButton.setVisibility(View.GONE); 


     return convertView; 
    } 

    public void setEditModeEnabled(boolean isEditModeEnabled) { 
     this.isEditModeEnabled = isEditModeEnabled; 
    } 

    private class ViewHolder { 
     TextView txtModel; 
     TextView txtColor; 
     TextView txtPrice; 
     ImageView imgCar; 
    } 
} 

,並在您EditButton的onClick()方法,你應該設置isEditModeEnabled您的適配器爲true,再刷新列表視圖這樣的:

public void onClick(View v) { 
    switch(v.getId()) { 
    case : R.id.btnEdit : 
    yourCustomAdapterInstance.setEditModeEnabled(true); 
    yourCustomAdapterInstance.notifyDataSetChanged(); 
    break; 
    } 
}