Reputation: 487
I have a custom adapter for my ListView which has multiple TextViews. I want to set an onItemClickListener to my ListView and extract text out of the TextViews. I tried using this:
String s =(String) ((TextView) mListView.findViewById(R.id.myNr)).getText();
but as expected by default it always returns the values from the first list item! (I guess because it doesn't specify the item id from the list)
I also tried this:
String s = (((TextView) mListView.getItemAtPosition(myItemInt)).
findViewById(R.id.myNr)).toString();
didn't work! Any suggestions?
This is implemented under the following function:
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView,
int myItemInt, long mylng) {
// String s statements
}
EDIT:
Here's the full code:
mListView_myentries.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView,
int myItemInt, long mylng) {
String workRequestSelected = (String) ((TextView) mListView_myentries.findViewById(R.id.work_request)).getText();
String activitySelected = (String) ((TextView) mListView_myentries.findViewById(R.id.activity)).getText();
String statusSelected = (String) ((TextView) mListView_myentries.findViewById(R.id.status)).getText();
String workRequestDescSelected = (String) ((TextView) mListView_myentries.findViewById(R.id.desc)).getText();
String actualHoursString = (String) ((TextView) mListView_myentries.findViewById(R.id.actual_hours)).getText();
}
Upvotes: 3
Views: 2377
Reputation: 1
This worked perfect for me, however when i called my listContacts(listview) directly it always gave me the first record but when i called the view passed in to me it worked like a charm.
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = (((TextView)view.findViewById(R.id.txtVwConID)).getText().toString());
Toast.makeText(getApplicationContext(), "you selected: " + item, Toast.LENGTH_SHORT).show();
If you have an question about my code ill post more here
Upvotes: 0
Reputation: 5203
Try the below code
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView,int myItemInt, long mylng) {
String s =(String) ((TextView) myView.findViewById(R.id.myNr)).getText();
}
}
Upvotes: 3