2017-05-25 61 views
-1

是否有可能綁定onClickListener的活動中的listView項目按鈕?我的listView項目有名稱,刪除和編輯按鈕。 listView項目存儲在列表中。是否可以在activity中爲自定義listView項目設置onClickListener?

exercises_list_view_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:orientation="vertical" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp"> 

    <Button 
     android:id="@+id/deleteExerciseBtn" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_alignParentRight="true" 
     android:background="@android:drawable/ic_delete" /> 

    <Button 
     android:id="@+id/editExerciseBtn" 
     android:layout_width="26dp" 
     android:layout_height="26dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="7dp" 
     android:layout_toLeftOf="@id/deleteExerciseBtn" 
     android:background="@drawable/edit_btn" /> 

    <TextView 
     android:id="@+id/exerciseName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="8dp" 
     android:layout_toLeftOf="@id/editExerciseBtn" 
     android:ellipsize="end" 
     android:maxLines="1" 
     android:text="This is a Large text with fill width" 
     android:textColor="#000000" 
     android:textSize="18sp" /> 

</RelativeLayout> 

代碼活動:

@Override 
public void onButtonClickListner(int position, String value) { 
    Toast.makeText(create_workout.this, "Button click " + value, 
      Toast.LENGTH_SHORT).show(); 

} 

List<WorkoutExercise> workoutExercises = workout.getWorkoutExercises(); 
ListView exercisesList = (ListView) findViewById(R.id.exerciseListView); 
WorkoutExerciseListAdapter listAdapter = new WorkoutExerciseListAdapter(this, R.layout.exercises_list_view_item, workoutExercises); 
listAdapter.setCustomButtonListener(create_workout.this); 
exercisesList.setAdapter(listAdapter); 

由於WorkoutExerciseListAdapted有大量的代碼我粘貼在這裏:https://pastebin.com/FYNVVQnS

我所試圖做的是: 如果用戶點擊編輯按鈕,我需要將運動對象傳遞給另一個活動進行編輯。 如果用戶點擊刪除按鈕,我需要從列表中刪除<該項目>和更新的ListView(與notifyDataSetChanged()?)

我試圖尋找通過計算器和谷歌的答案,但它要麼沒有工作或我的應用程序開始崩潰。 很少的嘗試例子: http://www.c-sharpcorner.com/UploadFile/9e8439/create-custom-listener-on-button-in-listitem-listview-in-a/ Adding an onclicklistener to listview (android) 等..

我將不勝感激任何意見。感謝您的時間。

回答

0

1.更新您的customButtonListener界面如下:

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

2.在適配器getView()方法集點擊收聽到editdelete按鈕:

@NonNull 
@Override 
public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) { 

    ............ 
    ................... 

    // Edit 
    viewHolder.editBtn.setOnClickListener(new OnClickListener() { 

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

    // Delete 
    viewHolder.deleteBtn.setOnClickListener(new OnClickListener() { 

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

    return convertView; 
} 

在您的活動中,將CustomButtonListener添加到您的ListView

A. Get item position from `onEditButtonClickListner()` and get `workoutExercise` object and pass it to another activity 
B. Get item position from `onDeleteButtonClickListner()` and delete item and upadte ListView. 

添加以下代碼在您的活動:

.......... 
    ................. 

    listAdapter.setCustomButtonListener(create_workout.this); 
    exercisesList.setAdapter(listAdapter); 

    exercisesList.setCustomButtonListener(new WorkoutExerciseListAdapter.customButtonListener() { 

     @Override 
     public void onEditButtonClickListner(int position, String value) 
     { 
      // Item 
      WorkoutExercise workoutExercise = workoutExercises.get(position); 

      // Do something with object workoutExercise 
     } 

     @Override 
     public void onDeleteButtonClickListner(int position) 
     { 
      // Delete 
      workoutExercises.remove(position); 

      // Update ListView 
      listAdapter.notifyDataSetChanged(); 
     } 
    }); 

希望這將有助於〜

+0

謝謝你的幫助。我結合了你的兩個例子,一切都像魅力一樣工作! – Lukenzo

0

如果您wan't到火上的每個列表項的任何事件你的列表視圖,然後你應該在你的BaseAdapter類中實現它。 在您的WorkoutExerciseListAdapter,在裏面getView()方法定義您的事件列表程序。

如果你需要得到click事件上的活動,然後實施和使用的接口,如果這樣的話在WorkoutExerciseListAdapter

MyNotifier listner; 

public interface MyNotifier 
{ 
    void OnButtonClicked; 
} 

public void setMyNotificationListner(MyNotifier listner) 
{ 
    this.listner=listner; 
} 

然後,當按鈕點擊

@Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
    Button b=(Button)v.getViewByID(<id>); 
    b.setOnClickListner(new OnClickListner... 
    { 
     listner.OnButtonClicked() 
    } 
    } 

At MainActivity

WorkoutExerciseListAdapter listAdapter = new WorkoutExerciseListAdapter(this, R.layout.exercises_list_view_item, workoutExercises); 
listAdapter.setMyNotificationListner(new MyNotifier {..//You get click calls here..};) 
相關問題