I have a ListView with a setOnItemClickListener and setOnItemLongClickListener.
Since 1 year no problem with it. But with Android 4.4 I'll get with a LongClick both Methods executed.
For example:
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0,View arg1,int arg2, long arg3){
ListView lv = (ListView) findViewById(R.id.listView1);
final String Name = lv.getAdapter().getItem(arg2).toString();
// Make sth on click
}});
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
// Make sth on longclick
}
});
On long click both will be executed. Is this a bug or a problem with my code?
There are no problem with your code it's just that you haven't returned a value on your onItemLongClick boolean. Change it into:
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
// Make sth on longclick
return true;
}
});
This will prevent long click on doing further actions since take note that a longclick is still a click which is why onclicklistener triggers on this event.
Related
I'm trying to pass the values that I have defined in a Spinner to a ListView. I mean when you select an item from this spinner shown in the ListView.
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int posicion, long arg3) {
String valor = spinner_datos[posicion].toString();
adapter.add(valor);
list.setAdapter(adapter);
}
Use adapter.notifyDataSetChanged() after the adapter.add(valor);
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int posicion, long arg3) {
String valor = spinner_datos[posicion].toString();
adapter.add(valor);
adapter.notifyDataSetChanged(); //This will notify the adapter to redraw it's views, updating the list so that you can see the changes
}
You don't need to call the list.setAdapter(adapter); again
I have 3 spinner in my app, and I set all by
spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_function_A();
}
});
spinner_3.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_function_B();
}
});
spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_functionC();
}
});
Code above, look run function if Spinner Selected item, but
i want to reset all spinner with button like this
clear_all_spinner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
spinner_1.setSelection(0);
spinner_2.setSelection(0);
spinner_3.setSelection(0);
}
});
I want after all reset with not running run_function_A(),run_function_B(),run_function_C() .. how the way?
you can try setting setOnItemSelectedListener(null) on all spinner before resetting the default selected item to the one at zeroth index and then setOnItemSelectedListener back to the one you set. I have modified you code as below:
clear_all_spinner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
spinner_1.setOnItemSelectedListener(null);
spinner_2.setOnItemSelectedListener(null);
spinner_3.setOnItemSelectedListener(null);
spinner_1.setSelection(0);
spinner_2.setSelection(0);
spinner_3.setSelection(0);
spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_function_A();
}
});
spinner_3.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_function_B();
}
});
spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_functionC();
}
});
}
});
Also i see one bug in your code, you are setting setOnItemSelectedListener for spinner_1 twice ... see run_functionA() and run_functionC() are both set for spinner_1 selection. i have copied your code as it but you need to modify it.
It would be better to set the code of setting up ItemSelectedListener in a method block so instead of writing the same code at two places, you can simply call the method.
I am developing an Android application in which I am using a listview and search bar. When I click on any searched item it will show starting items, not the clicked one.
Can anyone help me how to get the position of selected item?
You can use this for getting position of your selected Item
YourList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SelectedId = YourList.getItemIdAtPosition(arg2);}});
- Use getItemIdAtPosition() method.
Eg:
ListView lv = (ListView) findViewById(R.id.listView_ProductList);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
i = new Intent(ProductActivity.this, ProductDetails.class);
// i.putExtra("keyAapo", arg2 );
i.putExtra("keyAapo", (int) lv.getItemIdAtPosition(arg2));
startActivity(i);
}
});
First, while clicking the listview item,
get the position of that item in listview
Pass the strings/values to that intent if any.
Intent for navigating to next class as below:
YourListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor listItem = (Cursor)<YourListView>.getItemAtPosition(position);
String lID = listItem.getString(1);
Intent i= new Intent(<ClassName>.this, <Navigate to Class>.class)
i.putExtra("lID", lID); // key-value pair
startActivity(i);
}});
here is my click listner...
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.i("", "adfadf");
Toast.makeText(ScoreListActivity.this, "Clicked",Toast.LENGTH_SHORT).show();
}
});
Whats wrong in this code ....\
I am not Getting Click when I click an item of LISTVIEW...
Call the following code on your list items as they are created:
listItem.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
I know how can implement listview without using listactivity.But i dont know how get the clicked item from the listview.Please anyone tell me how listen the clicked item from the listview without using listactivity.
Please Follow this Link. Here you can find simple Listview example using Activity.
Using listview.setOnItemClickListener you can get Click on Listview.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
}
You can implements OnItemClickListener and can write the method like below or just simply use eclipse to implement unimplemented method
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
}
First you have to make instance of list view Clickable like
lv.setClickable(true);
then
use following
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
}
use ListView.
Then with the listview object invoke
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//do your stuff
}
}
for click on listview in activiyt try this code...
ListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//write you action here
}
});