在listview中选择任何列表项时不调用OnItemSelected方法

时间:2016-06-02 15:15:15

标签: android listview

我开始这个包含listview和edittext的活动(startactivityforresult)来搜索它。当用户单击列表项时,OnItemSelected应该将选定的列表项返回到上一个活动。我点击列表项,没有任何反应。为什么?

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_origin);

    i = getIntent();

    ListView lv = (ListView) findViewById(R.id.listView1);

    aa = new ArrayAdapter<String>(getApplicationContext(), R.layout.loginlist, orig);

    EditText et;

    lv.setAdapter(aa);

    et = (EditText) findViewById(R.id.editText1);

    et.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence cs, int start, int before, int count) {

            aa.getFilter().filter(cs);

        }

        @Override
        public void afterTextChanged(Editable s) {


        }

    });

    lv.setOnItemSelectedListener(new OnItemSelectedListener() {

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

            selected = orig[position];

            i.putExtra("selection", selected);
            setResult(RESULT_OK, i);
            finish();
        }

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

            setResult(RESULT_CANCELED, i);
            finish();

        }
    });


}
}

1 个答案:

答案 0 :(得分:1)

更改listview侦听器。

例如:

      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           //here you can apply your logic.
        }
    });