Listview and onItemLongClick doesn't work - java

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

Related

i don't understand why string comparison is returning false even when the strings are equal

s = ans.getText().toString();
jum.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
String selected = jum.getItemAtPosition(arg2).toString();
if(s.equals(selected))
{
//jum.setItemChecked(arg2, true);
ans.setText("correct");
}
else
{
ans.setText("incorrect");
}
}
});
Here ans is an EditText ans jum is a listview. I am trying to compare the string entered by user in edittext with the string of the item in listview that he clicks on.
jum.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String selected = jum.getItemAtPosition(arg2).toString();
s= ans.getText().toString();
if(s.equals(selected))
{
//jum.setItemChecked(arg2, true);
ans.setText("correct");
}
else
{
ans.setText("incorrect");
}
}
});

onItemLongClick is undefined?

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

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

How to separate what is called an onListItemClick when a list item is clicked?

I have a list of items that include a checkbox. There is text on the left side of a list item and a check box on the right side. When I click a list item the checkbox is clicked and it fires the rest of the intent in the onListItemClick method. How do I change my code so that when a check box is checked the onListItemClick does one thing and when the text is clicked, the onListItemClick does something else?
I have a method like this to refresh my list items:
public void refreshlist(){
mymap = null;
mymap = providerTester.downLoadinfo(value1,value2);
list_my = new ArrayList<String>(mymap.keySet());
adapter = new ArrayAdapter<String>(ClassName.this, android.R.layout.simple_list_item_multiple_choice, list_my);
//using builtin list_item
getListView().setChoiceMode(2);
setListAdapter(adapter);
}
And then I have an onListClickListener setup like this:
#Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
My_DownLoad my_dl = (My_DownLoad) map_thoughts.get(my_list.get(position));
Integer id1 = position;
Log.i("ListOthers", "onListItemClick position: " + id1.toString());
Long id2 = id;
Log.i("ListOthers", "onListItemClick position: " + id2.toString());
//For Some reason id2 and id1 are the same
//I was thinking about setting up a case statement if something different happens if I click the checkbox and not the text
//start activity if text is checked
//change state of checkbox if checkbox is clicked
}
OK, after a lot of trial and error, I got this to work. I am pretty happy to finally see what I was looking for. Here is what I ended up with:
public void refreshlist(){
mymap = null;
mymap = providerTester.downLoadinfo(value1,value2);
list_my = new ArrayList<String>(mymap.keySet());
adapter = new ArrayAdapter<String>(ClassName.this,android.R.layout.simple_list_item_multiple_choice, list_my);
//using builtin list_item
getListView().setChoiceMode(2);
setListAdapter(adapter);
}
and the second method
#Override
protected void onListItemClick(ListView l, View v, int position, final long id){
super.onListItemClick(l, v, position, id);
final My_DownLoad my_dl = (My_DownLoad) map_thoughts.get(my_list.get(position));
final Intent i = new Intent(this, NextActivity.class);
l.setOnItemLongClickListener(new OnItemLongClickListener(){
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3){
}
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//add values to intent
startActivity(i);
return false;
}
});
}

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