如何在ListView中触发setOnItemSelectedListener?

时间:2017-02-18 13:04:21

标签: android listview

我运行此代码打击, 和setOnItemClickListener()运行良好, 但无法触发所选事件。 是否有错误或ListView无法绑定setOnItemSelectedListener? 有人可以帮帮我吗?

public class MainActivity extends Activity{
private TextView selection;
private static final String[] items={"lorem", "ipsum", "dolor",
        "sit", "amet",
        "consectetuer", "adipiscing", "elit", "morbi", "vel",
        "ligula", "vitae", "arcu", "aliquet", "mollis",
        "etiam", "vel", "erat", "placerat", "ante",
        "porttitor", "sodales", "pellentesque", "augue", "purus"};

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    ListView listView = (ListView) findViewById(R.id.list);
    ArrayAdapter aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,items);
    listView.setAdapter(aa);
    listView.setChoiceMode(CHOICE_MODE_MULTIPLE);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selection.setText(items[position]);
        }
    });
    listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this,items[position],Toast.LENGTH_SHORT).show();
        }

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

        }
    });

    selection=(TextView)findViewById(R.id.selection);
  }
}

1 个答案:

答案 0 :(得分:0)

我猜您正在考虑保留所选项目,如果是这样,那么在xml列表视图部分中添加以下行

android:choiceMode="singleChoice"
 android:listSelector="#666666" 

你的活动应该是这样的事情

public class MainActivity extends Activity{
private TextView selection;
private static final String[] items={"lorem", "ipsum", "dolor",
        "sit", "amet",
        "consectetuer", "adipiscing", "elit", "morbi", "vel",
        "ligula", "vitae", "arcu", "aliquet", "mollis",
        "etiam", "vel", "erat", "placerat", "ante",
        "porttitor", "sodales", "pellentesque", "augue", "purus"};

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    ListView listView = (ListView) findViewById(R.id.list);
    selection=(TextView)findViewById(R.id.selection);
    ArrayAdapter aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,items);
    listView.setAdapter(aa);
    listView.setChoiceMode(CHOICE_MODE_MULTIPLE);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selection.setText(items[position]);
              Toast.makeText(MainActivity.this,items[position],Toast.LENGTH_SHORT).show();
        }
    });

  }
}

您还可以关注here

以外的其他方法