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)的第三个参数

int Position(从 0 开始),如果没有选择任何内容,则为 INVALID_POSITION。

getSelectedItemPosition如果项目被选中,getSelectedItem则返回值!= 1 和!= null。

的文档getSelectedItemPosition说:

如果没有选择任何内容,则返回 INVALID_POSITION。

于 2013-06-07T09:56:27.223 回答
0

Try this:

public class MainActivity extends Activity {

    ListView record_list;
    String[] arr = {"One", "Two", "Three"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        record_list = (ListView)findViewById(R.id.recordList);
        TextView tv = (TextView)findViewById(R.id.textView);
        ArrayAdapter adpt = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,arr);

        record_list.setAdapter(adpt);

        record_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {



            String st = (String)record_list.getItemAtPosition(arg2);

            System.out.println("View:"+ arg1 + "item at position:"+ st);
            }

        });
    }
}
于 2013-06-07T10:22:55.223 回答
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 回答
0
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)

这里变量 i 是项目在列表中的位置

正如我在此页面上看到的那样setOnItemClickListener(),还有setOnItemSelectedListener(). getSelectedItemPosition我猜只能在后一种情况下工作。

于 2013-06-07T10:00:16.883 回答