onItemLongClick is undefined? - java

I'm trying to implement a long click in my listview item but it doesn't work and i get an error that says is undefined. Here's the code:
protected void setOnItemLongClickListener(ListView l, View v, int position, long id) {
super.onItemLongClick(l, v, position, id);// Error
ApplicationInfo app = applist.get(position);
try {
Intent intent = packageManager
.getLaunchIntentForPackage(app.packageName);
if (null != intent) {
startActivity(intent);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
Someone has an idea how solve the problem?Thanks

The reason for this is most likely that you don't implement the listener. Something like
public class ActivityName extends Activity implements OnItemLongClickListener{
Try changing
protected void setOnItemLongClickListener
to
protected boolean setOnItemLongClickListener{
// your code
return true;
You need to use the proper return type for the method which is boolean then return true so the listener knows it was a success.
Docs

try this listener for Listview :
istView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(arg0.getContext(), ((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();
return false;
}
});

use this code
yourListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//YOUR_CODE_HERE
return false;
}
});

Please replace
public class MainActivity extends Activity implements OnItemLongClickListener
and add unimplemented method
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
by default you can do this by right clicking on OnItemLongClickListener select Quick fix

try to add this lines to your list adapter
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
and the method is try to overwrite your method
#Override
public boolean onItemLongClick(
AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
return false;
}

Related

Listview and onItemLongClick doesn't work

i've got a listview with all applications installed.. I need that onItemLongClick uninstall the application i click in the listview. The starting code for the onItemLongClick is this one:
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
And this is for uninstall:
ApplicationInfo app = applist.get(position);
Uri packageUri = Uri.parse("package:"+app.packageName);
Intent uninstallIntent =
new Intent(Intent.ACTION_DELETE, packageUri);
startActivity(uninstallIntent);
return true;
i need also insert some parameters and i tryied this one but i have an error in onItemLongClick:
protected boolean setOnItemLongClickListener(ListView l, View v, int position, long id) {
super.onItemLongClick(l, v, position, id);// Error
ApplicationInfo app = applist.get(position);
Uri packageUri = Uri.parse("package:"+app.packageName);
Intent uninstallIntent =
new Intent(Intent.ACTION_DELETE, packageUri);
startActivity(uninstallIntent);
return true;
}
how can i solve?
try to implement this
import android.widget.AdapterView.OnItemLongClickListener;
yourListView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "delete item in position : " + arg2, Toast.LENGTH_SHORT).show();
return false;
}
});

Android - Spinner + setOnClickListener

So, i want to assign a Spinner OnClick, but i get the following error
java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
I need it to collect data when i click the spinner, then use it later.
I know i did it before, but i lost the code. Googled for an answer, i didn't find anything that works.
If this helps, I am using pagerview layout.
Here is my code:
public class DATA extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
ScrollView GPU_LAYOUT = (ScrollView)inflater.inflate(R.layout.gpu, container, false);
final TextView higher_than =(TextView) GPU_LAYOUT.findViewById(R.id.gpu_higher_than_value);
final Spinner min_max =(Spinner) GPU_LAYOUT.findViewById(R.id.min_max_spinner_gpulay);
min_max.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String TMP=min_max.getSelectedItem().toString();
higher_than.setText(TMP);
}
});
return GPU_LAYOUT;
}
Is there any way i can do it? Thank you!
You can do it with setOnTouchListener in that way:
spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Toast.makeText(getBaseContext(), "CLICK", Toast.LENGTH_SHORT).show();
return false;
}
});
I'm doing it like this:
public void addListenerOnSpinnerItemSelection() {
mySpinner = (Spinner) findViewById(R.id.GPU_LAYOUT);
mySpinner.setOnItemSelectedListener(new myOnItemSelectedListener());
}
public class myOnItemSelectedListener implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long arg3) {
int position=Arrays.asList(getResources().getStringArray(R.array.GPU_LAYOUT)).indexOf(fecha); }
#Override
public void onNothingSelected(AdapterView<?> arg0) { }
}
Also I suggest to get the position of the group you're using and then handle it.
min_max.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
//whatever your logic is put it here
}
});
you can't use setOnClickListener directly on a Spinner. If you want to do it here is a trick.
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (view.getId()) {
case R.id.sp_category:
if (motionEvent.getAction() == MotionEvent.ACTION_UP)//replacing with onclick
{
//Perform any action
}
break;
}
return true;
}
In this code MotionEvent.ACTION_UP will make this touch act like onclick.
Hope it will help you!

how to implement a long click listener and onclicklistener in single listview

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

Single listclick does not get executed, but longclick does

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

Spinner with on Click Listener

I am using spinner that shows error when i am trying to extract the item id of the selected spinner item.
My Code goes here:
public void dispspi()
{
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter <String> adap= new ArrayAdapter(this, android.R.layout.simple_spinner_item, level);
spinner.setAdapter(adap);
spinner.setOnItemClickListener(new OnItemClickListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
int item = spinner.getSelectedItemPosition();
p=item;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
How to get the item id of the spinner? Any help is appreciated..Thanks in advance
IIRC, you should be using a selected listener, not click:
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
Then you can add the override tag to your selected method.
private String selecteditem;
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
selecteditem = adapter.getItemAtPosition(i).toString();
//or this can be also right: selecteditem = level[i];
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
}
});
spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int postion, long arg3) {
// TODO Auto-generated method stub
String SpinerValue3 = parent.getItemAtPosition(postion).toString();
Toast.makeText(getBaseContext(),
"You have selected 222 : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Yes you can use some OnItemSelectedListener for work with selected item. But sometimes we would like to handle exactly click for spinner. For example hide keyboard or send some analytics etc. In this case we should use TouchListener because OnClickListener doesn't work properly with Spinner and you can get error. So I suggest to use TouchListener like:
someSpinner.setOnTouchListener { _, event -> onTouchSomeSpinner(event)}
fun onTouchSomeSpinner(event: MotionEvent): Boolean {
if(event.action == MotionEvent.ACTION_UP) {
view.hideKeyBoard()
...
}
return false
}
you should have this in the listener(OnItemSelectedListener)
public void onNothingSelected(AdapterView<?> arg0) {
}
It might works without it but put it to be consistent
but there might be other errors also, can you provide the error log ?

Categories