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();
Related
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();
I have a ListView in my Android app, which it has some item with exactly same
name. How can distinguish which one is clicked? Is there any solution?
For example, I have retrieved these rows from database:
How do I send their ID and receive back their ID by selecting the name in ListView?
(I just want to display item's name)
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// use the position to distinguish the view clicked
});
}
With the position is:
plansList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int intPosition = position;
String clickedValue = plansList.getItemAtPosition(intPosition).toString();
}
});
String[] locations = {"russia", "ukraine", "brazil"};
locationList = new ArrayList<String>(Arrays.asList(locations));
search_listview_result = (ListView)findViewById(R.id.search_listview_result);
searchResultListViewAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, android.R.id.text1, locationList);
search_listview_result.setAdapter(searchResultListViewAdapter);
search_listview_result.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences.Editor editor = getSharedPreferences("SelectedLocation", MODE_PRIVATE).edit();
editor.putString("Name", locationList.get(position));
editor.putString("Lat", String.valueOf(locationLatList.get(position)));
editor.putString("Long", String.valueOf(locationLongList.get(position)));
editor.commit();
finish();
}
});
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();
}
});
I used list view to list the items of my SQLite database as follows:
listView = (ListView) findViewById(R.id.listView1);
db.open();
Cursor c = db.getAllHistory();
ArrayList<String> temparr = new ArrayList<String>();
if (c.moveToFirst()) {
do {
temparr.add(c.getString(0)+'\t'+c.getString(1)+'\t'+c.getString(2)+'\t'+c.getString(3)+'\t'+c.getString(4)+'\t');
} while(c.moveToNext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, temparr);
listView.setAdapter(adapter);
} else {
db.close();
}
Now I want to access the specific list item by using OnItemClickListener. How should i do it?
To get your perticular data-
Implement setOnItemClickListener()-
and position will return list's item position number:-
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String yourData = temparr.get(position);
}
});
Just call listView.setOnItemClickListener() with your implementation of the listener.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// your code
// Toast.makeText(context,temparr.get(position),Toast.LENGTH_SHORT).show();
}
});
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.
You can set the listener like this:
Put this code in onCreate()
TextView disp = (TextView) findViewById(R.id.textView1);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String temp = (String) listView.getItemAtPosition(position);
disp.setText(temp);// display on screen
}
});
EDIT:
As you want to display the listItem on screen in a permenant sort of way rather than displaying a toast, the easiest way that you can do this is by adding a TextView in your layout. See the updated code
Hope this helps.
I use the below code to create listview without listactivity.I need to know how to get the selected list item from this.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myList = new String[] {"Hello","TEST","TEST","TEST4"};
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
setContentView(lv);
}
Use onItemClickListener
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
http://d.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener%28android.widget.AdapterView.OnItemClickListener%29
set listener on the list
lv.setOnItemSelectedListener(this);
then implement it
public void onListItemClick(ListView parent, View v, int position, long id) {
selection.setText(items[position]);
}