0

just wanted to ask about this since I found it really weird and I can't tell why this was happening. Well I think the code will explain it all and I just wanted to know why does this happen.

 record_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        SELECTED_ITEM_ON_LIST = (String)(record_list.getItemAtPosition(i));



        Log.v("Selected adapterView", String.valueOf(adapterView.getSelectedItem()));
        Log.v("Selected adapterView", String.valueOf(adapterView.getFocusedChild()));
        Log.v("Selected item position", String.valueOf(record_list.getSelectedItem()));
        Log.v("Selected item position", String.valueOf(record_list.getSelectedItemPosition()));
        Log.v("adapterView", String.valueOf(adapterView.getCount()));
        Log.v("View", String.valueOf(view.isSelected()));
        Log.v("recordList", String.valueOf(record_list.getCount()));
        Log.v("Selected item", SELECTED_ITEM_ON_LIST);
    }

});

and the logs:

on first click for awesome2:

V/Selected adapterView: null
06-07 09:45:12.398    8488-8488/com.test.testaudio             V/Selected adapterView: null
06-07 09:45:12.408    8488-8488/com.test.testaudio             V/Selected item position: null
06-07 09:45:12.408    8488-8488/com.test.testaudio             V/Selected item position: -1
06-07 09:45:12.408    8488-8488/com.test.testaudio             V/adapterView: 14
06-07 09:45:12.408    8488-8488/com.test.testaudio             V/View: false
06-07 09:45:12.408    8488-8488/com.test.testaudio             V/recordList: 14
06-07 09:45:12.408    8488-8488/com.test.testaudio             V/Selected item: Awesome2

second click for awesome4:

V/Selected adapterView: null
06-07 09:45:37.099    8488-8488/com.test.testaudio             V/Selected adapterView: null
06-07 09:45:37.108    8488-8488/com.test.testaudio             V/Selected item position: null
06-07 09:45:37.108    8488-8488/com.test.testaudio             V/Selected item position: -1
06-07 09:45:37.108    8488-8488/com.test.testaudio             V/adapterView: 14
06-07 09:45:37.108    8488-8488/com.test.testaudio             V/View: false
06-07 09:45:37.108    8488-8488/com.test.testaudio             V/recordList: 14
06-07 09:45:37.108    8488-8488/com.test.testaudio             V/Selected item: Awesome4

as you can see the item position is always null for selected item and always -1 for selected item position. can anybody tell me why?

4

4 に答える 4

1

アイテムの位置はonItemClick(your int i)の 3 番目のパラメーターです。

int Position (0 から開始)、または何も選択されていない場合は INVALID_POSITION。

getSelectedItemPositiongetSelectedItem項目が選択されている場合は、値 != 1 および != nullを返します。

のドキュメントは次のようにgetSelectedItemPosition述べています。

何も選択されていない場合、INVALID_POSITION を返します。

于 2013-06-07T09:56:27.223 に答える
0
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)

ここで、変数 i はリスト内のアイテムの位置です

このページにあるようにsetOnItemClickListener()、 と もありsetOnItemSelectedListener()ます。getSelectedItemPosition私が推測する後者の場合にのみ機能します。

于 2013-06-07T10:00:16.883 に答える
0

usually I use a global array (the one used to fill the adapter) and then extract the i position from that, to get the needed value:

//filling the adapter
itemsAdapter = new ItemsAdapter(getApplicationContext(),
                R.layout.vedi_rubrica_riga, m_arrDati);

//on item click listener
list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                ApriRubrica(m_arrDati.get(position).get("id"));

            }
        });
于 2013-06-07T09:59:17.540 に答える