【发布时间】:2018-04-17 20:38:08
【问题描述】:
我在我的android程序中有一个ListView(在字符串数组中的预定义的引号集),在选择项目时似乎没有工作。 ListView 已填充,但我的 onClickListener 中的 toast 消息从未被调用。我不确定这里有什么问题。我以前从来没有遇到过这个问题。
这是我的 onCreate 方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
List<String> quotesList = new ArrayList<String>(Arrays.asList(quotes));
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotesList);
listView.setAdapter(arrayAdapter);
listView.setOnItemSelectedListener(new ListView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Toast.makeText(getApplicationContext(), "Toast", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {}
});
}
这是我的 activity_main.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b2cfff"
tools:context="com.examples.myteststuff.listviews.MainActivity">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:src="@drawable/icon"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:id="@+id/listView"
android:background="@drawable/border"
android:layout_marginHorizontal="8dp"
android:layout_marginVertical="8dp"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
【问题讨论】:
-
日志文件中是否有任何错误?
-
不。没有错误。
-
设置 OnItemClickListener 时会发生什么?
-
我认为
setOnItemSelectedListener可以在spinners上工作,你试过setOnItemClickListener吗?
标签: java android listview layout