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);
}
});
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;
}
});
basically I have 2 ListViews; Arrivals/Departures and when you click on the item in either list it would return the title however when I click anything in my "Departure" list it returns elements from my Arrivals list, here are my listeners! Any help would be appreciated
here is where I set the each list
ListView DeparturesListView = (ListView)findViewById(R.id.Departures);
ThreeHorizontalTextViewsAdapter threeHorizontalTextViewsAdapter1 = new ThreeHorizontalTextViewsAdapter(getApplicationContext(), R.layout.three_horizontal_textviews_layout, DeparturesList);
DeparturesListView.setAdapter(threeHorizontalTextViewsAdapter1);
ListView ArrivalsListView = (ListView)findViewById(R.id.Arrivals);
ThreeHorizontalTextViewsAdapter threeHorizontalTextViewsAdapter2 = new ThreeHorizontalTextViewsAdapter(getApplicationContext(), R.layout.three_horizontal_textviews_layout, ArrivalsList);
ArrivalsListView.setAdapter(threeHorizontalTextViewsAdapter2);
These are my listeners
ArrivalsListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
ThreeStrings temp2 = (ThreeStrings) ArrivalsListView.getAdapter().getItem(position);
TrainService ArrivalTrain = temp2.getTrain();
Log.d("templor", "ARRIVAL: "+ArrivalTrain.getOrigin().getLocationName().toString());
}
});
DeparturesListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
ThreeStrings temp = (ThreeStrings) DeparturesListView.getAdapter().getItem(position);
TrainService service = temp.getTrain();
Log.d("templor", "DEPARTURE"+service.getOrigin().getLocationName().toString());
}
});
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// my code here, but I don't know how..
}
}
I think it will be something like this:
get the selected item position of mySpinner
switch(pos)
Update spinner entries? I don't know...
I'm not sure what to do for the last step. Help?
try in this way
car.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
itemSelected = arg2;
// add items to the cardetails spinner's adapter using the itemselected and refresh the adapter using nofifyDataSetChanged()
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
cardetails.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
here car and cardetails are spinners
you need to refresh the adapter that is related to the car spinner so that the spinner also gets refreshed
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; }});
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 ;)