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
Related
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);
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();
}
});
i have a listview. i have to get the text of the list item which i clicked. i tried using onitemclick. but i am not able to get. can some 1 please explain me how to get it.
here is my onitemclick method
lstcities.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
// String str =(String) lstcities.getItemAtPosition(position);
String S =view.getContext().toString();
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + S, Toast.LENGTH_LONG)
.show();
thanks
ramu
try this,
setListAdapter(new ArrayAdapter<String>(this, R.layout.yourlayout,your list));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
});
Assuming you are using a TextView, you should be able to do
String text = parent.getItemAtPosition(position).getText().toString()
You can get string from ArrayList used in adapter.
like : String str = arrayList[position]; //Assume arrayList here is String[];
If your using customize view(own xml) for list item. You will get view of whole xml as 2nd parameter(View view) using that you can find out your textView like view.findViewById(your text view id);
If your using android define item android.R.layout.list_item_1 then typecast your view object with textview;
Using this textView you can get text of textView or like same manner any view will be accessible from list item.
Another way to do it is:-
lstcities.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id)
{
String text = (String) (((ListAdapter)parent.getAdapter).getItem(position));
// Do what you want to with this text
}
}
All you need to simply add below statement :
String selectedFromList = (String) (lv.getItemAtPosition(position));
I have an AutoCompleteTextView in my app which works. I have successfully created an onClickItemListener. The question is how to grab the text the user selected.
And this is the thing: I have an ArrayList with words being passed to the Adapter to search for suggestions. As the user types a word the suggestions list gets shorter (in rows on the UI side) so when i want to get the word from the ArrayList at the index the user selected i get the wrong word because the indexes doesn't match.
How can I get the text (String) the user chose without having to mess with the index?
Here's my code:
public class AutocompleteActivity extends BaseActivity {
private DBManager m_db;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete);
m_db = new DBManager(this);
final ArrayList<String> words = m_db.selectAllWords();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, words);
AutoCompleteTextView tv = (AutoCompleteTextView)findViewById(R.id.autocomplete);
tv.setThreshold(1);
tv.setAdapter(adapter);
tv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("SELECTED TEXT WAS------->", words.get(arg2));
}
});
}
}
Yeah... unfortunately the name of the parameters on the onItemClick method you must implement are not so self-descriptive but here is an example with the names of what they are:
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String)parent.getItemAtPosition(position);
//TODO Do something with the selected text
}
});
parent The AdapterView where the click happened.
view The view within
the AdapterView that was clicked (this will be a view provided by the
adapter)
position The position of the view in the adapter
id The row id of the item that was clicked.
For more info see: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html
arg0 being your AdapterView and arg2 the position.
Have you tried:
arg0.getItemAtPosition(arg2);
I think what you are looking for is this.
String s = this.mCountry.getEditableText().toString();
Where mCountry is the AutoCompleteTextView.
this.mCountry = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapterCountry = new ArrayAdapter<String>(this, R.layout.list_item, countries);
this.mCountry.setAdapter(adapterCountry);
mCountry is the list of countries, and I wanted to save the country selected in SharedPreferences.
Hope this helps.
Easiest of all
For Getting text of the selected suggestion in AutoCompleteTextView use this
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("========>>", autoCompleteTextView.getText().toString());
}
});
try this:
txtPurpose.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Purpose selected = (Purpose) arg0.getAdapter().getItem(arg2);
txtPurpose.setTag(selected);
}
});
One another of getting text of suggestion selected in AutoCompleteTextView is
#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);
}
Here is the code that will solve the problem.
private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
int index = (int) view.getTag();
Object item = parent.getItemAtPosition(index);
if (item instanceof SearchItemShareConnectionDAO) {
SearchItemShareConnectionDAO dao = (SearchItemShareConnectionDAO) item;
}
}
};
SetTag(Dao.getPosition) in getView() method of adapter.
To get the text of the displayed item selected by the user
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selectedItemText = arg0.getItemAtPosition(arg2);
Log.i("myTag", "SELECTED TEXT WAS["+selectedItemText+"]);
}
Following best practices, please use some form of descriptive nomenclature for your variables, your code will make more sense:
public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int selectedItemIndexIn, long id)
There is also a way to get item outside the onItemClick:
int index = tv.getListSelection();
if (index != ListView.INVALID_POSITION) {
Object item = tv.getAdapter().getItem(index);
}
beware that int getListSelection() may return ListView.INVALID_POSITION if there is no dropdown or if there is no selection.
For me, but on setOnItemSelectedListener():
arg0.getSelectedItem();
was enough. The same should work on setOnItemClickListener() I guess.
Kotlin version
autoCompleteId.setOnItemClickListener { parent, view, position, id ->
val selectedItem = parent.getItemAtPosition(position).toString()
Log.d("SELECTED ITEM", selectedItem )
}
Output:
D/CLASSROOM: selectedItem
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();