Reputation: 3625
I have a listview with items in it. Depending on which ListViewItem has been clicked I'd like to open an Activity and pass data to it. But how do I get the position of which ListViewItem has been clicked?
This is my code
public class tutorialActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial);
registerClickCallBack();
ListView listView = (ListView) findViewById(R.id.tutorialList);
String tutorialTitle1 = getResources().getString(R.string.tutorial1_title);
String tutorialTitle2 = getResources().getString(R.string.tutorial2_title);
String tutorialTitle3 = getResources().getString(R.string.tutorial3_title);
String tutorialTitle4 = getResources().getString(R.string.tutorial4_title);
String tutorialTitle5 = getResources().getString(R.string.tutorial5_title);
String tutorialTitle6 = getResources().getString(R.string.tutorial6_title);
String tutorialTitle7 = getResources().getString(R.string.tutorial7_title);
String tutorialTitle8 = getResources().getString(R.string.tutorial8_title);
String tutorialTitle9 = getResources().getString(R.string.tutorial9_title);
String tutorialTitle10 = getResources().getString(R.string.tutorial10_title);
String tutorialTitle11 = getResources().getString(R.string.tutorial11_title);
String tutorialTitle12 = getResources().getString(R.string.tutorial12_title);
String tutorialTitle13 = getResources().getString(R.string.tutorial13_title);
String tutorialTitle14 = getResources().getString(R.string.tutorial14_title);
String[] values = new String[] { tutorialTitle1, tutorialTitle2, tutorialTitle3, tutorialTitle4, tutorialTitle5, tutorialTitle6, tutorialTitle7, tutorialTitle8, tutorialTitle9, tutorialTitle10, tutorialTitle11, tutorialTitle12, tutorialTitle13, tutorialTitle14};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
}
private void registerClickCallBack() {
ListView list = (ListView) findViewById(R.id.tutorialList);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,int position, long id) {
//How do I retrieve this position that has been clicked?
}
});
}
}
Upvotes: 0
Views: 93
Reputation: 6438
Use getCheckedItemPosition() to determine the position of the selected item, assuming you have choiceMode:single.
http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemPosition()
To simply get the position of the item that has been clicked in the onClick method, well that is right there for you already.
private void registerClickCallBack() {
ListView list = (ListView) findViewById(R.id.tutorialList);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,int position, long id) {
//How do I retrieve this position that has been clicked?
int thePositionThatWasClicked = position;
}
});
Upvotes: 1
Reputation:
Easy one. Reference here : http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()
getSelectedItemPosition()
:)
Upvotes: 0
Reputation: 1046
Declare values outside of onCreate
String[] values;
then just assign a value to it:
values = new String[] { tutorialTitle1, ... };
and then in onItemClick
get string using values[position];
And start activity depending on that string.
Upvotes: 2
Reputation: 509
Use String selectedItem = (String) list.getSelectedItem(); to get selected String. Position of clicked item you have from method public void onItemClick(AdapterView parent, View viewClicked,int position, long id)
Upvotes: 1
Reputation: 139
Not sure what you are asking for, in the callback method you have position.
Upvotes: 0