【问题标题】:Android: ListView "setOnItemSelectedListener" not workingAndroid:ListView“setOnItemSelectedListener”不起作用
【发布时间】: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


【解决方案1】:

改用setOnItemClickListener

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               Toast.makeText(getApplicationContext(), "Toast", Toast.LENGTH_SHORT).show();
            }
        });

希望对你有帮助

【讨论】:

  • 是的,做到了。谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多