Android custom listview, setOnItemSelectedListener not working

≯℡__Kan透↙ 提交于 2019-12-04 19:30:52

问题


I'm just beginning Android development, and I'm working to get a Custom listview with a checkbox working. I've created a base class that extends Activity, Created an Adapter and overrode the getView() method to add the checkbox to the listview. I'm assuming I need to do this because I need something equivalent to didSelectRowIndexAtPath from Obj C to update my model. Please let me know if there's an alternate way of doing this too!

Now in my base class, I have the following code -

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout);
    setContentView(R.layout.facilityscreen);

    /* Static Data source */
    facilityModel = new FacilityDataModel[2];

    facilityModel[0] = new FacilityDataModel();
    facilityModel[1] = new FacilityDataModel();


    facilityModel[0].setFacilityName("Test 1");
    facilityModel[0].setFacilityID("Facid0001");
    facilityModel[0].setChecked(false);


    facilityModel[1].setFacilityName("Test 2");
    facilityModel[1].setFacilityID("Facid0002");
    facilityModel[1].setChecked(true);


    facilityListView = (ListView) findViewById(R.id.facilityListView);

    FacilityScreenAdapter adapter = new FacilityScreenAdapter(this, facilityModel);

    facilityListView.setAdapter(adapter);    

    myPatBtn = (Button) findViewById(R.id.myPatBtn);
    myPatBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            int i=0;
            i++;
        }});

    facilityListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            int i=0;
            i++;

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

}    

My problem now is the setOnItemSelectedListener isn't getting called at all. Been struggling with this for a couple of hours now, and I can't figure out why it wouldn't get called at all.

Any help is much appreciated!

Thanks,
Teja.


回答1:


I know this is an outdated answer but I'm going to write it just in case some other fellow who has the same "problem" bumps onto this page :

The solution to the above problem which is not a problem but just a misunderstanding is that the ListView.onItemSelected() event is fired up, upon :

1) Navigating through the emulators-cross handles or 2) as far-as my HTC-Hero is concerned, the rolling-action on the white little roller-ball.

You don't have to extend your activity explicitly to a ListActivity.

Here's my tiny little code which retrieves a phone number from a TextView control, inside a listview item. When the user either touches the list item or scrolls through the list with

the little roller-ball the below Events, fire up and MakeACall() method is called :

myList.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) 
            {
                TextView myPhone = (TextView)view.findViewById(R.id.txtphone);
                MakeACall(myPhone.getText().toString());        
            }
        });

        myList.setOnItemSelectedListener(new OnItemSelectedListener() 
        {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long i) 
            {
                TextView myPhone = (TextView)view.findViewById(R.id.txtphone);
                MakeACall(myPhone.getText().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

I hope that was helpful... :)




回答2:


There exists already the possibility to have a ListView with checkboxes.

public class List11 extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, GENRES));

        final ListView listView = getListView();

        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }


    private static final String[] GENRES = new String[] {
        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
    };
}

I've taken this from the APIDemos 'cause it was the simplest. You can then get the selected items by using:

long[] selectedIds = getListView().getCheckItemIds();

What you may also be interested in is the CheckedTextView which is used internally in the list.

To the part of the onListItemClick problem
Try to extend from ListActivity rather than Activity. Then override the onListItemClick. That should work.




回答3:


You should set all focusable items in custom list layout to false:

        android:focusable="false"

also I think you should not use attributes like android:clickable="true" for them.




回答4:


use

setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// here you code 
}
})

instead of setOnItemSelectedListener

As setOnItemSelectedListener is called when item is being selected not clicked so to get clicked item you must use setOnItemClickListener this will work




回答5:


The lack of the item selected listener getting called is by design and is based on which mode the device is in. In touch mode, there is no focus and no selection. Your UI should use widgets that differentiate between touch for selection versus touch for scrolling. Radio buttons, for example, are good when there is a single selection choice.



来源:https://stackoverflow.com/questions/3630468/android-custom-listview-setonitemselectedlistener-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!