Reputation: 4371
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?
Upvotes: 0
Views: 91
Reputation: 763
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);
}
});
}
}
Upvotes: 0
Reputation: 818
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
here variable i is the position of the item in the list
As I see on this page there is setOnItemClickListener()
and also setOnItemSelectedListener()
. getSelectedItemPosition
will only work in latter case I guess.
Upvotes: 0
Reputation: 516
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"));
}
});
Upvotes: 0
Reputation: 157487
the item position is the third parameter of onItemClick
(your int i
)
int Position (starting at 0), or INVALID_POSITION if there is nothing selected.
getSelectedItemPosition
and getSelectedItem
returns a value != 1 and != null if the item isSelected.
The doc for getSelectedItemPosition
says:
returns INVALID_POSITION if there is nothing selected.
Upvotes: 1