2017-07-04 1 views
-2

funktioniert ich einen String Arraylist und eine Zeichenfolge arrayAdapter bin mit einem Listview zu füllen, das ist meine XML-Datei:listview.setOnItemSelectedListener nicht

<LinearLayout 
       android:id="@+id/llHoursD" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:descendantFocusability="blocksDescendants" 
       android:visibility="visible" 
       android:weightSum="1">       

       <TextView 
        android:id="@+id/tvDay" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="15dp" 
        android:layout_toRightOf="@+id/ivName" 
        android:background="@null" 
        android:enabled="false" 
        android:gravity="center_horizontal" 
        android:inputType="textPersonName" 
        android:text="Fecha Seleccionada" 
        android:textColor="@color/category" 
        android:textColorHint="@color/category" 
        android:visibility="visible" 
        android:textSize="17sp" />  

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:layout_marginTop="10dp" 
       android:scaleType="centerCrop" 
       android:src="@drawable/separator" />  

      <ListView 
       android:id="@+id/lvReservationTime" 
       android:layout_width="match_parent" 
       android:layout_height="163dp" 
       android:divider="@null" 
       android:dividerHeight="8dp" 
       android:layout_marginTop="8dp" 
       android:visibility="visible" 
       android:layout_weight="1.99"> 
      </ListView>  

      </LinearLayout> 

Dies ist das Layout, in dem die Textview, die in der Liste enthalten ist

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/txtItemAva" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

ist

befindet Und dies ist der Code in das Fragment

listHourAvali = new ArrayList<>(); 
      adapterListHourAvali = new ArrayAdapter<String>(getActivity(),R.layout.detail_list_reservation,R.id.tvAvailiHours,listHourAvali); 
      mLvReservationTime.setAdapter(adapterListHourAvali); 

      mLvReservationTime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
       @Override 
       public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

        Toast.makeText(getActivity(),"Prueba",Toast.LENGTH_LONG).show(); 
       } 

       @Override 
       public void onNothingSelected(AdapterView<?> parent) { 

       } 
      }); 

die Liste gefüllt ist Bei einer anderen Methode besteht das Problem darin, dass das Drücken eines Elements in der Liste nicht in setOnItemSelectedListener geht. Was ist das Problem hier?

+0

Mögliche Duplikat [Android benutzerdefinierte listview, setOnItemSelectedListener funktioniert nicht] (https://stackoverflow.com/questions/3630468/android-custom-listview-setonitemselectedlistener-not-working) – Selvin

Antwort

1

Sie müssen vorsichtig sein, etwas zu wählen ist eine andere Aktion als etwas zu klicken.

sehen: OnItemSelectedListener vs OnItemClickedListener

Wenn Sie eine Aktion machen wollen, wenn ein Listenelement geklickt wird (= ausgewählt!), Dann sollten Sie die OnClickListener verwenden:

mLvReservationTime.setOnItemClickListener(new OnItemClickListener() 
{ 
    @Override 
    public void onItemClick(AdapterView<?> adapter, View v, int position, 
     long arg3) 
    { 
     // do what you intend to do on click of listview row 
     Toast.makeText(getActivity(),"Prueba", Toast.LENGTH_LONG).show();  
    } 
});