Detect long click on a list view of two items

Let me start by saying that I am new to Android development, but have a strong background in C #, VB.

I read a ton of posts on similar issues and tried solutions, but it never works. I am sure that this is what I am doing, and it is based on my ignorance of the programming language.

So, I have the code for the ListAdapter to populate a list view of two items.

import java.util.ArrayList; ... public class ListAdapter extends BaseAdapter { private Activity activity; private ArrayList<Integer> image; private ArrayList<String> list1; private ArrayList<String> list2; private static LayoutInflater inflater=null; public ListAdapter(Activity a, ArrayList<Integer> image, ArrayList<String> list1, ArrayList<String> list2) { this.activity = a; this.image = image; this.list1 = list1; this.list2 = list2; ListAdapter.inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { return image.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public static class ViewHolder{ public TextView text1; public TextView text2; public ImageView image; } public View getView(int position, View convertView, ViewGroup parent) { View vi=convertView; ViewHolder holder; if(convertView==null){ vi = inflater.inflate(R.layout.grid_list_layout, null); holder=new ViewHolder(); holder.text1=(TextView)vi.findViewById(R.id.item1); holder.text2=(TextView)vi.findViewById(R.id.item2); holder.image = (ImageView)vi.findViewById(R.id.icon); vi.setTag(holder); } else holder=(ViewHolder)vi.getTag(); holder.text1.setText(this.list1.get(position)); holder.text2.setText(this.list2.get(position)); holder.image.setImageResource(this.image.get(position)); return vi; } 

}

I modified the above to use ArrayList instead of Array in the original article. It is working fine. What I want to do is to detect a long click and prompt the user to delete or not.

There is this incomplete code in the main.xml file, which is a list view

 <ListView android:id="@+id/lvResult" android:layout_width="fill_parent" android:layout_height="296dp" android:paddingLeft="10px" android:paddingRight="10px" > </ListView> 

There is another grid_list_layout.xml file containing

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/icon" android:layout_width="72px" android:layout_height="wrap_content" android:layout_marginTop="5px" /> <TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/twolinelist" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick" android:orientation="vertical" android:paddingBottom="5px" android:paddingTop="5px" > <TextView android:id="@+id/item1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/item2" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="30px" android:textAppearance="?android:attr/textAppearanceSmall" /> </TwoLineListItem> 

In the main action, it is created (?) Using

 ListView lv = (ListView) findViewById(R.id.lvResult); //listAdapter is declared further up listAdapter = new ListAdapter(HomesterActivity.this, arrIcon, arrSSID, arrMAC); lv.setAdapter(listAdapter); 

So, what would I like to know how to intercept a long click? Where can I put the various bits? those. there must be something in the XML file, but which one? I tried

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onListItemClick(ListView l, View v, int position,long id) { //super.onListItemClick( l, v, position, id); Toast.makeText(this, position, Toast.LENGTH_LONG).show(); } } 

and different versions, but nothing like the compiler!

If you have the time and am so inclined, I would appreciate some education so that I can figure out around it.

+6
source share
2 answers

To get long clicks, use setOnItemLongClickListener instead . Like this:

 lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { public boolean onItemLongClick(AdapterView<?> parent, View v, int position,long id) { //do stuff in here. } }); 
+9
source
 lv.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(final AdapterView<?> parent, View v, final int position, long id) { //do stuff with the item you long clicked on listAdapter.getItem(position); ... } }); 

ps. don't forget to import the correct class:

 import android.view.View.OnLongClickListener; 
+2
source

Source: https://habr.com/ru/post/902589/


All Articles