Reputation: 367
I have a listView, I'm wondering how to get the position of the item selected in the ListView so that I can use this code under the OnItemClick method.
string selected = listView.getItemPosition().toString()
So then I can use the if / else clause or switch to say:
if (selected.positionEquals("0")) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
I hope I made sense, please note, the listView.getItemPosition().toString() and selected.positionEquals was something I made up so you can get an idea of what I want.
Thanks!
EDIT
I just saw this from another question, I searched before asking this, but this just popped out. Do you think it will work in my case?
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 1)
{
Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
startActivityForResult(myIntent, 0);
}
if(position == 2)
{
Intent myIntent = new Intent(YourActivity.this, ThirdActivity.class);
startActivityForResult(myIntent, 0);
}
}
});
Upvotes: 1
Views: 5158
Reputation: 3485
yes working for you this way
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 1)
{
Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
startActivityForResult(myIntent, position);
}
if(position == 2)
{
Intent myIntent = new Intent(YourActivity.this, ThirdActivity.class);
startActivityForResult(myIntent, position);
}
}
});
Also switch to will work for you, and if you want compare using String like
string selected = listView.getItemPosition().toString()
then do like
if (selected.equalsIgnoreCase("0")){
// do work here
}else if(selected.equalsIgnoreCase("1")){
// do work here
}
end so on.
Upvotes: 0
Reputation: 896
Using Below code Toast
has print the position of selected item :-
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),"Position of Selected Item is :-"+position,Toast.LENGTH_LONG).show();
}
});
Upvotes: 0
Reputation: 5295
please see the code below :-
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int index,
long arg3) {
if (index == 0)) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
}
});
Upvotes: 0
Reputation: 1477
Try this code:
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
}
});
here "int position" will gives the selected item index value.
Upvotes: 0
Reputation: 7745
in one of the parameter of onItemClick
there is a position value (third argument) of the clicked item. you dont need to get it progromatically, just use that value to know
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) { //check third argument it automatically get the selected item POSITION
// TODO Auto-generated method stub
Intent i;
switch (position)
{
case 0: // first one of the list
i = new Intent(this, anActivity.class);
startActivity(i);
break;
case 1: // second one of the list.
i = new Intent(this, anActivity2.class);
startActivity(i);
break;
// and so on...
}
}
Upvotes: 3