AutoCompleteTextView click listener not working - android

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

Related

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

Tooltip on Listview Item click listner don't work

Tooltip on Listview Item click listner don't work
This Library add my project
compile 'com.github.douglasjunior:android-simple-tooltip:0.2.0'
final SimpleTooltip tooltip = new SimpleTooltip.Builder(v.getContext())
.anchorView(v)
.text("Results")
.dismissOnOutsideTouch(true)
.dismissOnInsideTouch(false)
.contentView(R.layout.tooltip,R.id.tv_result_header)
.textColor(getResources().getColor(R.color.green))
.gravity(Gravity.BOTTOM)
.build();
tooltip.show();
ArrayList<Result_ModelList> Result_list = new ArrayList<Result_ModelList>();
Listview lv=(Listview)tooltip.findViewById(R.id.lv);
Result_list.add(new Result_ModelList("Alphabetically"));
Result_list.add(new Result_ModelList("By Code"));
Result_list.add(new Result_ModelList("Last created"));
Result_Adapter adapter = new Sort_Result_Adapter(getApplicationContext(), R.layout.raw_result,Result_list);
lv.setAdapter(adapter);
//**Do not work**
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "do not work" , Toast.LENGTH_SHORT).show();
}
});
Did you see the Usage section?
Listview lv=(Listview)tooltip.findViewById(R.id.lv);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
new SimpleTooltip.Builder(this)
.anchorView(view)
.text("Something")
.gravity(Gravity.END)
.animated(true)
.transparentOverlay(false)
.build()
.show();
}
});
If you want to show it on a particular item on ListView you can use
View v=lv.getChildAt(position);
and use it as anchor view
anchorView(v)
to show it on first item do this:
ArrayList<Result_ModelList> Result_list = new ArrayList<Result_ModelList>();
Listview lv=(Listview)tooltip.findViewById(R.id.lv);
Result_list.add(new
Result_ModelList("Alphabetically"));
Result_list.add(new Result_ModelList("By Code"));
Result_list.add(new Result_ModelList("Last created"));
Result_Adapter adapter = new Sort_Result_Adapter(getApplicationContext(), R.layout.raw_result,Result_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "do not work" , Toast.LENGTH_SHORT).show();
}
});
View view=lv.getChildAt(0);
SimpleTooltip tooltip = new SimpleTooltip.Builder(v.getContext())
.anchorView(view) //if you want to display on whole listview do .anchorView(lv)
.text("Results")
.dismissOnOutsideTouch(true)
.dismissOnInsideTouch(false)
.contentView(R.layout.tooltip,R.id.tv_result_header)
.textColor(getResources().getColor(R.color.green))
.gravity(Gravity.BOTTOM)
.build();
tooltip.show();

Android listview setOnItemClickListener

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_campaign_list);
Campaign campaign_data[] = new Campaign[]
{
new Campaign(R.drawable.hlbb, "MSIG HLBB PA Extra"),
new Campaign(R.drawable.hlbb, "MSIG HLBB SSP Plus")
};
CampaignAdapter adapter = new CampaignAdapter(this, R.layout.listview_header_row, campaign_data);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
I can run the code above without setonitemclicklistener function, when i add the setOnItemClickListener, it stopped working.What is the error?? I'm new in Android..Thx
I don't know how is your CampaignAdapter code, but you are trying to cast the row view which you are inflating into TextView. You should use your adapter to access the item using the position of element:
CampaignAdapter adapter = new CampaignAdapter(this, R.layout.listview_header_row, campaign_data);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Campaign item = (Campaign) adapter.getItem(position);
// Now you can access to the campaing value that you want
// For instance, item.getText()
Toast.makeText(getBaseContext(), item.getText(), Toast.LENGTH_LONG).show();
}
});

how to call function in setOnItemClickListener

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

Get a field from a ListView

I have an Activity with ListView took in my Database. I would like to, when I select one, get the id field.
lv = (ListView) findViewById(R.id.listMessageConversationView);
Cursor c = selectInfoInDB();
int[] to = new int[] {R.id.idMessageClavier, R.id.nomMessageClavier, R.id.valeurMessageClavier, R.id.groupeMessageClavier, R.id.occurrenceMessageClavier};
SimpleCursorAdapter sCA = new SimpleCursorAdapter(this, R.layout.conversation_clavier_display_data, c, SmartAccess_v1Activity.nomColonnesMessage, to);
lv.setAdapter(sCA);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView)findViewById(R.id.idMessageClavier);
}
});
The fields in the ListViews are correct, but the toast give me the id of the first item of ListView, wherever I touch on list.
I worked on that and i I can't figure where is the mistake -_-
Thanks for your help, korax
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int postion,
long id) {
Toast.makeText(this, "id is :: " + id +"position :: " + position,
Toast.LENGTH_SHORT).show();
}
});

Categories

Resources