how to call function in setOnItemClickListener - android

i'm implementing autocomplete, the goal is when a word is selected, i do not have to click the button, it'll automatically continue, following is my code
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.editText1);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
Logger.getLogger("test").info("selected");
postData(this);
}
});
the log information is correct, and postData(View view)is the onclick function of the button, but there's error:The method postData(View) in the type MainActivity is not applicable for the arguments (new AdapterView.OnItemClickListener(){}). so how to call function inside the setOnItemClickListener?

Change the code to,
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
Logger.getLogger("test").info("selected");
postData(arg1);
}
});

Related

AutoCompleteTextView click listener not working

My autoCompleteTextView's click listener isn't working but I don't why.
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.userstatuslayout, R.id.listusername, values);
editText.setThreshold(1);
editText.setDropDownAnchor(R.id.districtsearch);
editText.setAdapter(adapter);
editText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selection=(String)parent.getItemAtPosition(position);
Toasty.info(getApplicationContext(),selection).show();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.userstatuslayout, R.id.listusername, values);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id...);
textView.setAdapter(adapter);
}
}
And your ItemClickListener should be:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(AutoCompleteActivity.this,
"Clicked item from auto completion list "
+ adapterView.getItemAtPosition(i)
, Toast.LENGTH_SHORT).show();

error while trying to set onItemClickListener in ListView up

error screenshot
I am trying to make clickable items in ListView, and recivieng this kind of error.
ListView chHE = (ListView) findViewById(R.id.lvHE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.support_simple_spinner_dropdown_item, cities);
chHE.setAdapter(adapter);
chHE.getOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long l) {
if (position == 0) {
toast.show();
}
}
});
Error:(52, 13) error: method getOnItemClickListener in class AdapterView
cannot be applied to given types;
required: no arguments
found:
reason: actual and formal argument lists differ in length
where T is a type-variable:
T extends Adapter declared in class AdapterView
chHE.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
if(position==0){
toast.show();
}
}
});
maybe you have write in a mastake?change get=>set
you should remplace getOnItemClickListener to setOnItemClickListener
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
Try doing this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//doSomeThing
}
});

Set onItemClick listener for ListView in Android

I'm trying to set an OnItemClickListener for my ListView in Android, but i can't get it to work.
This is what i have so far:
public class MenuFragment extends SherlockFragment
{
ArrayList<Item> items = new ArrayList<Item>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
ListView list = (ListView)view.findViewById(R.id.list_mainmenu);
// some code here where i add items to an ArrayList...
// Then i add the ArrayList to an EntryAdapter
EntryAdapter adapter = new EntryAdapter(this.getActivity().getBaseContext(), items);
list.setAdapter(adapter);
list.setClickable(true);
list.setOnItemClickListener(AdapterView.OnItemClickListener()) {
// ...
}
}
But this gives me an error on OnItemClickListener():
The method OnItemClickListener() is undefined for the type AdapterView.
So my qyestion is, how can i set an OnItemClickListener on my ListView??
check this code
listView.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();
}
});
make sure you have imported correct packages:
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
try this
list.setOnItemClickListener(new AdapterView.OnItemClickListener()) {
// ...
}
You should implement a customAdapter for having more control on your listView, Here is the link after visiting this you should be able to do what is required. Or you can have this code to quickly do what you need.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long itemID) {
}
});
Try this:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// ...
}
});
try this one
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});

Check selected item of ListView

I have a ListView and need to check, which item has been clicked.
The way I have it, it only shows my the selected item when I use the scrollwheel of my mouse (when testing it in the Eclipse emulator). When I test it on a real mobile device, it doesn't react at all.
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
myFunction(listView.getSelectedItem());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Use setOnItemClickListener instead of setOnItemSelectedListener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//get value for listItems using position
String str =listItems[position];
myFunction(str);
}
});
I think better you need to use on onListItemClick listener
public void onListItemClick(ListView parent, View v, int position, long id)
{
parent.setItemChecked(position, parent.isItemChecked(position));
Toast.makeText(this, "You have selected" + items[position],30).show();
}

How to get text from AutoCompleteTextView?

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

Categories

Resources