我使用字符串arrayList和字符串arrayAdapter来填充listView,这是我的xml文件:
<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>
这是列表中的文本视图所在的布局
<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>
这是片段中的代码
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) {
}
});
列表中填写了另一种方法,问题是按下列表中的项目不会进入setOnItemSelectedListener
。这有什么问题?
答案 0 :(得分:2)
你必须要小心,选择某些东西与点击某些东西不同。
请参阅:OnItemSelectedListener vs OnItemClickedListener
如果要在单击列表项时执行某些操作(!=选中),则应使用onClickListener:
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();
}
});