我想获取 ListView 的 SelectedItem 索引。我通过点击项目来选择项目。OnItemClick 事件工作正常,但 getSelectedItemPosition() 返回 -1。
我做错了什么?
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" />
在活动中:
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();
}
});
对不起我的英语不好