getting item's name when clicking a ListView - android

Is there any way I can capture a name of an item clicked in a list view when using "onItemLongClickListiner" ? I know I can capture position etc but I need a name

I suppose that you ListView is filled up with String object:
public boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id) {
String name = (String) parent.getItemAtPosition(position);
}
AdapterView.getItemAtPosition(position) gets the data associated with the specified position in the list.

You can use lv.getItemAtPosition(position) to get the text based on the index position
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3)
{
String name = arg0.getItemAtPosition(position).toString()
}
});
For more info
getItemAtPosition

Try this..
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//inflated row layout textview
TextView tagText = (TextView) view.findViewById(R.id.txt_text);
String tag = tagText.getText().toString();
Toast.makeText(getApplicationContext(),
"Element Name " + tag + " Clicked", Toast.LENGTH_SHORT).show();
}
});

It has worked for me.
listtview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
String selected = (String) adapterView.getItemAtPosition(i);
Toast.makeText(getApplicationContext(),"This is"+selected,Toast.LENGTH_LONG).show();
}
});

Related

How to get values from the selected item in a list view?

This is my code so far. This is working but it only shows the very first value I have in the listView , not the selecting item by the user. Please halp me with this. This is really important.
unitListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
unitListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
TextView t1 = (TextView) findViewById(R.id.lblLUnitName);
String name=t1.getText().toString();
// String a= (String) unitListView.getItemAtPosition(position);
// touchpressed=position;
Toast.makeText(getApplicationContext(), "Selected Item :" +name, Toast.LENGTH_SHORT).show();
}
});
Update your code as follows and try ;
unitListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
unitListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
TextView t1 = (TextView) findViewById(R.id.lblLUnitName);
String name=String.valuesOf(position);
Toast.makeText(getApplicationContext(), "Selected Item :" +name, Toast.LENGTH_SHORT).show();
}
});
View is just to show some values on UI. If you want to get real values use position to get a value from your ListAdapter.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
YourItemClass item = (YourItemClass)listAdapter.getItem(position);
//[...]
}
Other way is to get ListAdapter from your unitListView directly:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = unitListView.getAdapter().getItem(position);
//[...]
}
Replace
TextView t1 = (TextView) findViewById(R.id.lblLUnitName);
By
TextView t1 = (TextView) view.findViewById(R.id.lblLUnitName);

how to find the position of item in a AutoCompletetextview filled with Array

I have an array (array1 with States) and AutoCompleteTextview in which I'm filling it with array1. When I select the value from AutocompleteTextView I select a state from AutoCompleteTextView Dropdown
What I want is to get the position of the item from array1 which I've selected.
What I've tried is OnClickEvent of AutocompelteTextView.
STATE.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String) parent.getItemAtPosition(position);
String num = selection;
}
});
It ss giving me the value which I selected from dropdown but i want to get the position of the value in array1. For Example I have an array of size 4, like array1 = {A,B,C,D}, and if I select B it should return me B position in Array i.e 2.
I hope I made it clear. Thanks in advance.
Use the position variable in the onItemClick.
STATE.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String) parent.getItemAtPosition(position);
int pos = -1;
for (int i = 0; i < yourarray.length; i++) {
if (yourarray[i].equals(selection)) {
pos = i;
break;
}
}
System.out.println("Position " + pos); //check it now in Logcat
}
});
Use List instead of Array. Implement onItemClickListener for AutoCompleteTextView, then use indexOf on your list to find the index of selected item.
actvCity.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int index = cityNames.indexOf(actvCity.getText().toString());
// Do Whatever you want to do ;)
}
});
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
TextView txtvw=(TextView) view;
String str=txtvw.getText().toString();
int index = contactNames.indexOf(str);
}
Item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//// id contains item if from database
ItemNoSelected = id;
}
});
id is from database, we can use that
You can find the parent string in the clickItem listener.
OSchools in this case is the custom ArrayList.
Object item = parent.getItemAtPosition(position);
for(OSchools d : data){
Log.e("name",d.getName());
String name = d.getName();
if(d.getName() != null && name.contains(item.toString())){
ID = d.getID();
}

How to get a TextView id from a custom adapter

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();
}
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();
}
}
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

how to get position of clicked item in OnClickListener of Listview?

I want to get selected listview item onclicked listner of listview.
Right now I have implemented onItemClickListener but when I click on an item text then it is raised. I want to raise it on a listview row click.
Any idea how to achieve this? To get the text value of the list onClick listener?
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value = lv1.getAdapter().getItem(position).toString();
//display value here
}
});
Try using this code.
lv1.setOnItemClickListener(new OnItemClickListener() { ![enter image description here][2]
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value = lv1.getItemAtPosition(position).toString();
//display value here
}
});
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int pos = parent.getPositionForView(view);
Toast.makeText(this,pos + "",Toast.LENGTH_SHORT).show();
}
If your ListView items were populated from an array words using an ArrayAdapter (which is hopefully) and implemented using a class Word with kind of this constructor:
public class Word {
private String mValueA;
/** Image resource ID for the word */
private int mAudioResourceId;
/*
* Create a new Word object.
*/
public Word(String valueA, int audioResourceId) {
mValueA = valueA;
mAudioResourceId = audioResourceId;
}
/*
* Get the default translation of the word
*/
public String getValueA() {
return mValueA;
}
/**
* Return the audio resource ID of the word.
**/
public int getAudioResourceId() {
return mAudioResourceId;
}
}
And then you can set setOnItemClickListener to access clicked item for example to play audio defined in array for this list item.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//like this:
Word word = words.get(position);
//and use this position to play the audio from the array
//corresponding to its item
mMediaPlayer = MediaPlayer.create(SomeActivity.this, word.getAudioResourceId());
mMediaPlayer.start();
}
});
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
int pos = (int) adapterView.getPositionForView(view);
Toast.makeText(MainActivity.this, pos, Toast.LENGTH_SHORT).show();

How to get the value of a Listview item which is clicked in android?

I have this below code access the ListView item value into string and display it in alert?
ListView shot = getListView();
shot.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
String S = arg1.getContext().toString();
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage(S).show();
}
maybe this example will help you
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
https://developer.android.com/reference/android/widget/ListView.html
This gives you the exact value of the item clicked. Check the log
ListView shot = getListView();
shot.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
String val =(String) parent.getItemAtPosition(position);
System.out.println("Value is "+val);
}
To get the value of your model
adaptor.getItem(position).getCardName();
Maybe you can try this
String data = (String)shot.getItemAtPosition(arg2);
AlertDialog.Builder adb = new AlertDialog.Builder(arg1.getContext());
adb.setMessage(data).show();

Categories

Resources