OnClick listerner seems to stop OnLongClick? - java

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.

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

Why is the wrong ListView item highlighted?

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

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 check if the user chose a choice in a spinner

I have a spinner that opens programaticly, and when the user chose an option from the spinner, it closes... is there a way to be notified, or a listener that tells you, when the user chose his choice?
the onItemSelected gets the default item that is chosen automaticly when the spinner is open.
set setOnItemSelectedListener to your spinner...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object obj = (Object) parent.getSelectedItem();
//get clicked position from position
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
//this method is call when nothing choosed by you
}
});
Add an OnItemSelectedListener to your Spinner.
I am not sure if I understand your question correctly but let me try to answer it. The normal and straight forward way would be to add an OnItemSelectedListener to the spinner i.e
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// Do whatever you want here
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
but this is such a basic thing that I feel like a fool for pointing it out. Anyway here is a tutorial on spinner from Android Developer resources. It creates the listener in step 5 and adds it to spinner on step 6.

Clear focus after click in item autocomplete

I have a problem. If I set clearFocus() in setOnItemClickListener it's not work.
Focus set nearby input.
How I can clear focus in autocomplete, after I clicked item?
textView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
arrAuto = (String) ((TextView) view
.findViewById(R.id.item_auto)).getText();
findViewById(R.id.editText1).clearFocus(); //it's not work
}
});
You need to set android:windowSoftInputMode="stateHidden" to your activity in AndroidManifest in order to keep the keyboard hidden when the activity is started.

Categories