Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Long Click on Two Item List View

Tags:

android

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

I have read a ton of posts on similar issues and tried the solutions but it never seems to work. I am certain it is something I am doing and it is based on my ignorance of the programming language.

So, I got some code for a ListAdapter to populate the Two Item List View.

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 have modified the above to use ArrayList instead of Array in the original article. It works fine. What I want to do is detect the long click and prompt the user to delete or not.

The main.xml has this partial code which is the 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 which contains

<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 activity it is instantiated(?) with

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 I would like to know is how do I intercept a long click? Where do I put the various bits? i.e. there must be something in the XML file, but which one? I have 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 various versions, but nothing seems to please the compiler!

If you have time and feel so inclined I would appreciate some education so I can get my mind around it.

like image 615
Tim Cowan Avatar asked Nov 28 '11 20:11

Tim Cowan


2 Answers

To get long clicks, you 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.
    }
});
like image 115
Kurtis Nusbaum Avatar answered Sep 28 '22 19:09

Kurtis Nusbaum


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. make sure to import the right class:

import android.view.View.OnLongClickListener;
like image 26
gwvatieri Avatar answered Sep 28 '22 17:09

gwvatieri