2017-07-28 57 views
0

我知道他們是類似的問題,但我不能讓他們的工作。我有一個列表片段,其中包含我的數組列表中的項目以及它旁邊的按鈕,用於數組列表中的每個元素。我的最終目標是讓程序只在用戶點擊按鈕時才作出響應,但我甚至無法檢測到屏幕上的點擊。我也嘗試將按鈕的焦點設置爲false(從其他問題中提出),但這也不起作用。這是我的代碼。自定義列表片段禁用點擊元素和啓用按鈕點擊(Android Studio)

public class ResultListFragment extends ListFragment { 
    private List<String> listValues, keyValues; 
    private String email, username; 
    private ArrayAdapter<String> myAdapter; 
    private ListView myListView; 

    private TextView title; 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_resultlist, container, false); 
     return view; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     View v = getView(); 
     myListView = getListView(); 

     listValues = new ArrayList<String>(); 

     myAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), 
       R.layout.fragment_rowlayout, R.id.myListText, CameraActivity.resultList); 
     setListAdapter(myAdapter); 
     myAdapter.notifyDataSetChanged(); 


    } 
    @Override 
    public void onListItemClick(ListView l, View v, final int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Log.d("blabla", "onListItemClick: clicked to : "+position); 
     final String delete=CameraActivity.resultList.get(position); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
      builder.setCancelable(true); 
      builder.setTitle("DELETION"); 
      builder.setMessage(delete + " delete it."); 
      builder.setPositiveButton("Onayla", 
        new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          Toast.makeText(getActivity().getApplicationContext(), delete+ "has been deleted", Toast.LENGTH_SHORT).show(); 
          CameraActivity.resultList.remove(position); 
          myAdapter.notifyDataSetChanged(); 
          for(String st:CameraActivity.resultList){ 
           Log.d("TAG", "onClick: eleman: " +st); 
          } 
         } 
        }); 
      builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
       } 
      }); 

      AlertDialog dialog = builder.create(); 
      dialog.show(); 

    } 
} 

這裏是我的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="match_parent" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp"> 

    <ListView android:id="@id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:drawSelectorOnTop="false"/> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="8dp" 
     android:background="@drawable/cembutton" 
     android:text="Yükle" 
     android:id="@+id/load" 
     android:layout_alignRight="@+id/results" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:layout_alignParentBottom="true" 
     android:textColor="#ffffff" 
     android:textStyle="bold"/> 

    <TextView android:id="@id/android:empty" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="No data"/> 
</RelativeLayout> 

和fragment_rowlayout

<?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="match_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    > 
     <TextView 
      android:id="@+id/myListText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="16dp" 
      android:layout_marginTop="16dp" 
      android:textStyle="bold" 
      android:textColor="#3700ff" /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:drawable="@drawable/cembutton" 
      android:layout_alignParentRight="true" 
      android:text="Çıkart" 
      android:layout_marginRight="50dp"/> 

    </RelativeLayout> 

回答

0

你應該爲你的listview提供custom adapter
然後,在getView()方法中,您可以通過id找到您的按鈕並將它設置爲onClickListener