Pergunta

I want to get SelectedItem index of ListView. I select item by tapping on item. OnItemClick event work fine, but getSelectedItemPosition() return -1.

What I do wrong?

xml:

    <ListView
    android:id="@+id/lvAddEdtList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:choiceMode="singleChoice"
    android:drawSelectorOnTop="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:listSelector="?android:attr/listChoiceIndicatorSingle" />

In Activity:

 lvAddEdtDel.setAdapter(namesList);
 lvAddEdtDel.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            lvAddEdtDel.setSelection(position);
            view.setSelected(true);

        }
    });

public void onClick(View v) {
if(lvAddEdtDel.getSelectedItemPosition() < 0 )
{ 
    Toast.makeText(getApplicationContext(), getString(R.string.ItemNotSelected),Toast.LENGTH_LONG).show();                          
}
});

sorry for my English

Foi útil?

Solução

I solved the problem by myself. Layout of the adapter has chosen: android.R.layout.simple_list_item_1;

In my case, it was necessary to choose: android.R.layout.simple_list_item_single_choice;

In touch mode must yuse getCheckedItemPosition

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top