Why is the wrong ListView item highlighted? - java

I implemented a ListView, and the method OnItemLongClick to highlight the selected item, but when I select an item, if I scroll the ListView, I find that another item is highlighted.
I'm using this code:
list.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
arg1.setBackgroundColor(Color.BLUE);
return true;
}
});

you can use android:listSelector property instead of onItemLongClick

Related

How to determine which Listview or Adapter is using an OnClick Method. Android

I want to use an OnClick method for multiple ListViews and Adapters. Inside my code I would like to check which adapter or listview click instantiated the method call. Here is what I was trying to no avail
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(parent==listView1.getParent()) {
If you have multiple ListView, you need to know which ListView is being clicked. So you need to check it first.
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.listView1: // this is ID in XML layout
// do action for ListView 1;
break;
case R.id.listView2: // this is ID in XML layout
// do action for ListView 2;
break;
default:
break;
}
}
You can try this.
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value = list.get(position);
//Action when clicked
Toast.makeText(MainActivity.this, value, Toast.LENGTH_LONG).show();
}

OnClick listerner seems to stop OnLongClick?

I have a listview in which previously every item was clickable, and it worked fine. But as soon as I added a feature to have all items long clicked, the onClick feature stopped working. I stripped down all my code for debugging purposes which I will submit here:
listview = (ListView) findViewById(R.id.workout_listview);
listview.setAdapter(new WorkoutListViewAdapter(this, new String[] {...}));
listview.setClickable(true);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.print("CLICK");
}
});
listview.setLongClickable(true);
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("LONG CLICK");
return true;
}
});
This code causes nothing to happen when a list item is clicked and displays "LONG CLICK" when one is long clicked.
I found this question, which seems to have the same problem but the code is structured very differently, and my code is already following the answer's guidelines of implementing the listener on the ListView rather than the row.

v7 searchview overlapping issue

example to what kind of problem I am running into.
Let say I have 6 elements in a listview all in order from left to right [1,2,3,51,4,5].
When I search for 5 it displays "51" and "5". When I click on "51" it opens up "1". The problem is that the filtered list overlaps the listview that displays all the elements. I was wondering how I can fix this issue. I am using v7.widget.SearchView
#Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.recipe_menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.search_recipe).getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
lv.clearTextFilter();
} else {
lv.setFilterText(newText.toString());
}
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
I think that you make mistake in onItemClick(AdapterView<?> parent, View view, int position, long id) method. You take data by position from data list. The "position" is a position of clicked item on list. So if you filtered your list and click on first item the value of position will be "1". You have to take value from adapter which is corresponding to clicked position. You should take item from adapter like is shown in code below:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
...
String data = ((ExerciseDAO)parent.getAdapter().getItem(position)).getTitle() ;
...
}
});
If you have questions - just ask.

Android: make a single item in a listview clickable [duplicate]

This question already has answers here:
Android - How to create clickable listview?
(5 answers)
Closed 8 years ago.
I currently have a ListView id=listview, which I am populating with an ArrayList id=myarraylist. Here is how I do it:
final ListView listview = (ListView) findViewById(android.R.id.list);
myarraylist = (ArrayList<String>) getArray();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);
myarraylist.add(...And then some other stuff I add...);
//This is the part I want to focus on
myarraylist.add("Reset High Scores");
adapter.notifyDataSetChanged();
listview.setAdapter(adapter);
Now, I want the entry Reset High Scores in my ListView to be clickable, and when it is clicked, I want it to do Some Stuff. I know for buttons all you do is set an onClickListener
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
..Some Stuff..
}
});
My question is, how can I make Some Stuff happen when the element Reset High Scores is clicked in the ListView
Edit: This is what I have so far
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#Override
String myString = (String) parent.getAdapter().getItem(position);
if(myString.equals("Clear High Scores")) {
//Stuff
}
}
});
ListView, just like Button, has the ability to set a click listener, but for ListView, the name of the method is setOnItemClickListener(). Instead of using OnClickListener, though, it uses a class called OnItemClickListener, which is more advantageous for using with ListView. The method looks as follows:
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id)
{
// some stuff
}
}
)
Now the advantage of this is that you can check what's contained at the position you've pressed, so the way you could tackle your problem could look something like this:
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id)
{
String myString = (String) parent.getAdapter().getItem(position);
if(myString.equals("Reset High Scores"))
{
// Do what you want
}
}
});
/**
* Creates a new instance
*/
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
super.onListItemClick(l, v, position, id);
//Do stuff
}
that should get ya started... Andrew is right though.

how to use OnItemLongClickListener without setOnItemLongClickListener in Android?

I am trying to use OnItemLongClickListener for a listView on Android. This code works fine when added to onCreate method.
mContactList.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("MyApp", "get onItem Click position= " + position);
return false;
}
});
However when I try to implement OnItemLongClickListener interface and use this method in the class:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("MyApp", "get onItem Click position= " + position);
return false;
}
nothing happens. What am I missing?
Did you register your object as listener, eg. setOnItemLongClickListener(this)?
You always have to set an setOnItemLongClickListener.
If you extract the onClick listener to another class than you have to set this OnItemLongClickListener to the listview.
e.g.
mContactList.setOnItemLongClickListener(new MyClassOnLogItemClickListener());
or if you are in the same class register it with this.

Categories