-1

我想改變TEXT按鈕上的按鈕onclicklistener但對於每5行顯示TEXT按鈕相同的結果。我只是想更改文本按鈕1個按鈕點擊的ListView自定義ArrayAdapter - 改變從按鈕TEXT按鈕OnClickListener

XML佈局childview

<Button 
    android:id="@+id/btn_seatmap" 
    style="@style/buttonSeatMap" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/txt_kereta_tiba" 
    android:layout_toRightOf="@+id/txt_info_seat" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:text="@string/btn_seatmap" />  

自定義適配器XML佈局的ListView

<ListView 
    android:id="@+id/list_kereta" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/btn_pilih_jadwal" 
    android:layout_below="@+id/txt_title_jadwal_kai" 
    android:layout_marginBottom="10dp" 
    android:background="@android:color/white" 
    android:choiceMode="singleChoice" 
    android:dividerHeight="1dp" 
    android:focusable="true" 
    android:scrollbars="none" />  

這裏代碼

@Override 
public View getView(final int arg0, View arg1, ViewGroup arg2) { 
    View view = arg1; 
    ListHolder listHolder = null; 

    if (view == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     view = inflater.inflate(layoutResourceId, arg2, false); 

     listHolder = new ListHolder(); 
     listHolder.layout = (RelativeLayout) view 
       .findViewById(R.id.layout_jadwal_kai); 
     listHolder.txtNama = (TextView) view 
       .findViewById(R.id.txt_nama_kereta); 
     listHolder.txtBerangkat = (TextView) view 
       .findViewById(R.id.txt_kereta_brkt); 
     listHolder.txtHarga = (TextView) view 
       .findViewById(R.id.txt_harga_kereta); 
     listHolder.txtTiba = (TextView) view 
       .findViewById(R.id.txt_kereta_tiba); 
     listHolder.txtClass = (TextView) view 
       .findViewById(R.id.txt_kelas_kereta); 
     listHolder.txtSeat = (TextView) view 
       .findViewById(R.id.txt_info_seat); 
     listHolder.btnSeatMap = (Button) view 
       .findViewById(R.id.btn_seatmap); 
     CheckSeatOnProgress = false; 
     view.setTag(listHolder); 
    } else { 
     listHolder = (ListHolder) view.getTag(); 
    } 
     listHolder.btnSeatMap.setTag(listHolder); 
     listHolder.btnSeatMap.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       ListHolder holder = (ListHolder) v.getTag(); 
       holder.btnSeatMap.setText("TEST"); 

       // listHolder.btnSeatMap.setText("TEST"); 
      }); 

適配器代碼

public void refreshListJadwal() { 
    adapter = new AdapterListKereta(getActivity(), 
      R.layout.custom_list_jadwalkai, setGetJadwals, dataJadwalKereta); 
    listKereta.setAdapter(adapter); 
} 

「TEST」 顯示,每5行。 請幫忙。如果你想點擊,那麼你可以使用ArrayList後更改按鈕的文字謝謝

+0

郵適配器代碼 – Raghavendra

+0

@Raghavendra我已經更新了我的問題 –

+0

在onclick u能嘗試這樣呢? ((按鈕)v)的.setText( 「TEST」); ? – Raghavendra

回答

0

我發現了一個解決辦法,而不是一個最佳的解決方案,雖然:d試試這個,

private HashMap<Integer, String> selected = new HashMap<Integer, String>(); 

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

     View view = arg1; 
     ListHolder listHolder = null; 

     if (view == null) { 
      LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
      view = inflater.inflate(R.layout.list_item, parent, false); 

      listHolder = new ListHolder(); 
      listHolder.nameTextView = (TextView) view.findViewById(R.id.listitem1); 
      listHolder.btnSeatMap = (Button) view.findViewById(R.id.listbtn1); 
      view.setTag(listHolder); 
     } else { 
      listHolder = (ListHolder) view.getTag(); 

     } 

     if(selected != null && !selected.isEmpty() && selected.get(position) != null) 
      listHolder.btnSeatMap.setText("TEST"); 
     else { 
      listHolder.btnSeatMap.setText(""); 
     } 

     listHolder.btnSeatMap.setTag(position); 
     listHolder.nameTextView.setText(itemsArrayList[position]); 
     listHolder.btnSeatMap.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       int which = -1; 
       Object obj = v.getTag(); 
       if (obj instanceof Integer) 
       { 
        which = (Integer) obj; 
       } 
       Log.e("POSISITON", which+""); 
       selected.put(which, "TEST"); 
       notifyDataSetChanged(); 
      }}); 

     return view; 
    } 
+0

不管它是什麼,每5行顯示TEXT按鈕相同的文字,它解決了我的問題。謝謝@Raghavendra –

+0

很高興!這有幫助。 – Raghavendra

0

1)設定位置作爲標記按鈕

2)當按鈕點擊,則找到位置form標籤

3)由位置獲得從創建的ArrayList相應文本和設置該文本到點擊視圖。

+0

listHolder.btnSeatMap.setTag(爲arg0); int posButton =(Integer)v.getTag(); btnCheck.setText(data.get(posButton).getnmKereta()); 還是同樣的結果,對於 –