I have a listview with some items. I would like to get the text from the selected item.
Here is my list adapter and the onItemClickListener:
ListView
Here's for future reference for anyone that stumbles on this.
In my case I had a custom adapter class, with type as a POJO class I had. Also the items I wanted to pass to the adapter and display in my ListView
where of the util.List
class.
I successfully passed the data to the ListView, but also wanted to get the text of the currently selected.
Eg: the data I passed was a list of schools that a lecturer taught at, so he had to select the particular school he wanted to work with at that time, and on logging in I wanted to pass an intent to a new Activity
with the current school the lecturer had selected.
Thus my ListView onClick():
private void loginSuccess() {
progressDialog.dismiss();
if (mySchoolsList.size() > 1) {
schoolsListView = new ListView(MainActivity.this);
schoolsArrayAdapter = new SchoolListAdapter(MainActivity.this, android.R.layout.simple_list_item_1, mySchoolsList);
schoolsListView.setAdapter(schoolsArrayAdapter);
dialog = new Dialog(MainActivity.this);
dialog.setContentView(schoolsListView);
dialog.setTitle("Welcome " + staff.getFullName());
dialog.show();
schoolsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//the .getName() is accessed from the School POJO class.
String schoolName = schoolsArrayAdapter.getItem(position).getName();
intent = new Intent(MainActivity.this, NavMainActivity.class);
intent.putExtra("sentIntent", schoolName);
startActivity(intent);
}
});
} else {
intent = new Intent(MainActivity.this, NavMainActivity.class);
intent.putExtra("sentIntent", recieveName);
startActivity(intent);
}
}
Hope this saves someone someday, because all the solutions here didn't work for me. Cheers!
It will definitly works!Hope you Satisfied!
ListView lv=(ListView)findViewById(R.id.listView1);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String text = (String) lv.getItemAtPosition(arg2);
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
});
Try this code:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),(String)parent.getItemAtPosition(position),Toast.LENGTH_SHORT).show();
}
});
public void onItemClick(**AdapterView**<?> parent, View view,
int position, long id) {
}
See the AdapterView
.....--->>>the class
Only need to do this :
TextView selectedText=(TextView) parent.findViewById(R.id.textView2);
See...you get the TextView
directly
Hello I'm using a CustomListView with Registered Context Menu. In this case the way to access a item inside a custom list row will be:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.add:
TextView textView = (TextView) info.targetView.findViewById(R.id.yourItem);
String text = textView.getText().toString();
Toast.makeText(getApplicationContext(), "Selected " + text, Toast.LENGTH_LONG).show();
default:
return super.onContextItemSelected(item);
}
}
Where R.id.yourItem is the textView inside the custom Row
PS: It's my first post, Hope it helps ;-)
For this you need to write the following:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView textView = (TextView) view.findViewById(R.id.list_content);
String text = lv.get(position).toString().trim();
System.out.println("Chosen Country = : " + text);
}});