I'm having a ListFragment with a list, that does not get single clicks. But long clicks are recognised.
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(null, "single click does NOT work.");
}
});
// contextual action bar (CAB).
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(null, "does work.");
}
});
I had a similar problem and was resolved by invalidating the listview's views when created and after scroll
listView.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
if ( scrollState == OnScrollListener.SCROLL_STATE_IDLE )
{
listView.invalidateViews();
}
}
#Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {}
});
I hope this helps
Make your activity extends from Activity and not ListActivity or something like that ;)
Related
protected void onListItemClick (ListView l, View v, int position, long id)
The normal method works fine for me but is there also a method for a long click?
Since I am on a ListActivity I cant use:
listview.setOnItemLongClickListener(new OnItemLongClickListener()
because i cant initialize listView bc of ListActivity.
OK I found a nice solution for it:
ListView lv = getListView();
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int row, long arg3) {
System.out.println("REIHE "+row);
return true;
}
});
holder.spinner_teacher_name=(Spinner) rowView.findViewById(R.id.spinner_teacher_name);
holder.spinner_teacher_name=(Spinner) rowView.findViewById(R.id.spinner_teacher_name);
arrayAdapter=new ArrayAdapter<String>(rowView.getContext(), android.R.layout.simple_spinner_item,teacher_name);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spinner_teacher_name.setAdapter(arrayAdapter);
holder.spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(rowView.getContext(), "You Clicked "+class_room[position], Toast.LENGTH_SHORT).show();
}
});
You should be used selected listener, not click listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
String selected_item = parent.getItemAtPosition(pos).toString();
Log.d("TAG", "_log : item_selected : " + selected_item);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
When the user selects an item from the drop-down, the Spinner object receives an on-item-selected event.
To define the selection event handler for a spinner, implement the AdapterView.OnItemSelectedListener interface and the corresponding onItemSelected() callback method.
See documentation.
Use:
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(rowView.getContext(), "You Clicked " + class_room[position], Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Instead of:
holder.spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(rowView.getContext(), "You Clicked "+class_room[position], Toast.LENGTH_SHORT).show();
}
});
Is there a way that I can get the previously selected item from a ItemSelected event on a spinner? My first thought was to respond to the OnItemClickedEvent, but that method is not supported for spinners.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
MyItem current = (MyItem) parent.getItemAtPosition(position);
//MyItem previous = (MyItem) parent.getPrevious ... how
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
//do nothing
}
});
I'm aware I can just cache the previous value, but it seems like a common operation that there should be a method for.
Use a variable to track the previous item. It would likely be at member of the Activity.
MyItem previous, current;
...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
previous = current
current = (MyItem) parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
//do nothing
}
});
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
int prev = comboValues.getSelectedItemPosition();
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
prev=position;
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
}
);
Note that nothing happens if you select an item you have already selected
Is it possible to set a background on the clicked/selected listitem (for multiplescreen/tablet)?
Yes, use SetOnItemClickListener.
list = (ListView)findViewById(R.id.listView1);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
//ListView parent, View v, int position, long id
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
arg1.setBackgroundColor(Color.RED);
}
});
I am creating a app in android. In that i am using list view. now i want use both click event and long click event. if is possible can any help me to do.
You just need to return true
list.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(final AdapterView<?> p, View v,final int po, long id) {
// your code
return true;
}
});
It basically tells the system that the Long press event has been handled (default is false), and no further events need to be handled (i.e. a single press, which inadvertently would happen in a long press event)
see this
Click & Long-Press Event Listeners in a ListActivity
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
onListItemClick(v,pos,id);
}
});
..
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
return onLongListItemClick(v,pos,id);
}
});
You should use ListView.setOnItemClickListener for a simple click.
For the long click, you have a choice. If you want to perform a single action use ListView.setOnLongClickListener. If you want a context menu then register the list for a context menu, create the menu and the actions for it.
registerForContextMenu(ListView);
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// menu code here
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// menu habdling code here
return super.onContextItemSelected(item);
}
use ListView.setOnItemClickListener(listener) and ListView.setOnItemLongClickListener(listener)
http://developer.android.com/guide/topics/ui/layout/listview.html
Just use setOnItemClickListener() and setOnItemLongClickListener() on your listview.
listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener()
{
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long arg3)
{
}
});
use the following code.
list.setOnItemClickListener(this);
list.setOnItemLongClickListener(this);
Listener definitions will be :
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
itemToclick is the visible portion on which click u want some action
itemToClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do your logic on click
});
itemToClick.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// do your logic for long click and remember to return it
return true; }});