how to get a list item position by clicking the button inside it?

32,358

Solution 1

ListView with Clickable Buttons!!!

Well...., here's the rough method to solve my problem SO FAR....

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:orientation="horizontal"
            android:layout_height="wrap_content">
<TextView android:id="@+id/showTv"
      android:layout_alignParentLeft="true"
      android:gravity="center_vertical"
      android:textSize="24dip"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content" /> 
 <RelativeLayout android:id="@+id/jjjj"
                 android:layout_alignParentRight="true"
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content">        
      <Button android:id="@+id/gointoBt"
               android:focusable="false"
               android:layout_alignParentRight="true"
               android:text="abc"
               android:layout_height="wrap_content"
               android:layout_width="wrap_content"/> 
      <Button android:id="@+id/chooseBt"
              android:layout_toLeftOf="@id/gointoBt"
              android:layout_marginRight="10dip"
              android:text="text"
              android:focusable="false"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"/>
  </RelativeLayout> 

MySimpleAdapter:

import ........;

public class MySimpleAdapter extends SimpleAdapter {

private final Context context;
private List<Map<String, Object>> data;
private int resource;
private String[] from;
private int[] to;

public MySimpleAdapter(Context context,List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
   super(context, data, resource, from, to);
   this.context=context;
   this.data=(List<Map<String, Object>>) data;
   this.resource=resource;
   this.from=from;
   this.to=to;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

   LayoutInflater inflater = ((Activity)context).getLayoutInflater();
   View rowView = inflater.inflate(resource, null, true); 
   Map<String, Object> medMap=data.get(position);
   final TextView[] showTv=new TextView[from.length]; 

   for (int i = 0; i < from.length; i++) { 
    showTv[i]=(TextView)rowView.findViewById(to[i]);
    showTv[i].setText(""+medMap.get(from[i]));
   }
Button btn=(Button)rowView.findViewById(R.id.gointoBt);
   Button.OnClickListener mOkOnClickListener = new Button.OnClickListener()
      {
         public void onClick(View v) {
         Log.v("ttttttt", ""+showTv[0].getText());
         Toast.makeText(context,""+showTv[0].getText(), Toast.LENGTH_LONG).show();
         }
     };
 btn.setOnClickListener(mOkOnClickListener); 

     Button btn2=(Button)rowView.findViewById(R.id.chooseBt);
   Button.OnClickListener mOkOnClickListener2 = new Button.OnClickListener()
      {
          public void onClick(View v) {
          Log.v("hhhhhhh", ""+showTv[0].getText());
          Toast.makeText(context,"abc"+showTv[0].getText(), Toast.LENGTH_LONG).show();
          }
      };
   btn2.setOnClickListener(mOkOnClickListener2);     
   return rowView; 
}
}

Activty:

import .......;

public class   ActivityMain extends Activity {

ListView listview;
List<Map<String,Object>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setTitle("My work");
   prepareData(); 
   listview =new ListView(this);
   MySimpleAdapter adapter=new MySimpleAdapter(this,data,R.layout.item,new String[]      {"uu"},new int[]{R.id.showTv});

   listview.setAdapter(adapter);                            
     setContentView(listview);

}
private void prepareData(){
   data=new ArrayList<Map<String,Object>>();
   Map<String,Object> item;
   item=new HashMap<String,Object>();
   item.put("uu", "hello");
   data.add(item);
   item=new HashMap<String,Object>();
   item.put("uu", "myyou");
   data.add(item);
   item=new HashMap<String,Object>();
   item.put("uu", "piero");
   data.add(item);
}

}

Thanks for that Man that is so kind to provide this AWESOME yet Mightiest tutorial.... which anyone here couldn't give to the noobs....

Solution 2

Actually, I used the same approach - just added casting the parent layout and I got the position w/o any exception

public void deleteButtonClick(View v) {
        //TODO Remove favorite - DB + file system
        Toast.makeText(this, "Deleting bookmark", Toast.LENGTH_SHORT).show();
        final int position = getListView().getPositionForView((LinearLayout)v.getParent());
        if (position >= 0) {
            Favorite o = (Favorite) this.getListAdapter().getItem(position);
        }
}

My layout for listview row :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.bbox.application"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.markupartist.android.widget.ActionBar
        android:id="@+id/actionbar" app:title="@string/ac_title" style="@style/ActionBar" />
    <ListView android:id="@+id/android:list" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_weight="5"
        android:background="@drawable/categrory_bckgr" />
    <TextView android:id="@+id/android:empty"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:text="@string/main_no_items" />

</LinearLayout>

I hope it'll help.

Solution 3

We can get the position by implementing onClickListener inside the getView method of adapter.

ADAPTER :

public class DetailsListAdapter extends BaseAdapter {
Context context;
ArrayList<Profile> data;

public DetailsListAdapter(Context context, ArrayList<Profile> data) {
    this.context = context;
    this.data = data;
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.list_item_logs_details, null);
        viewHolder = new ViewHolder();
        viewHolder.btn = (Button) convertView.findViewById(R.id.log_details_1_a);
        viewHolder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Log.d("statusPosition","position "+position);
            }
        });
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

public static class ViewHolder {
    TextView tv2, tv3, tv3a, tv4, tv4a;
    Button btn;
}
}

Solution 4

An easier solution to this probably is when your Activity implements OnClickListener and you set the (casted) Context as OnClickListener to any view you want in the Adapter. For more robust code you can check with instanceof.

public class MyActivity implements OnClickListener {

    // ...

}

public class MyAdapter extends CursorAdapter {

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        final TextView tv = (View) view.findViewById(R.id.text1);

        setOnClickListener((OnClickListener)context);

        tv.setTag(cursor.getPosition());

    }

}

For convenience you can set the items position in the adapter as Tag of the view. That way you can easily lookup the whole item in the adapter.

Solution 5

I think one more best approach is there.You can add one tag as position with the button using (button.setTag()) method and whenever user will click button just take the tag of this button. it will more easy.

Thanks and Regards Rahul

Share:
32,358

Related videos on Youtube

BolbazarMarme
Author by

BolbazarMarme

Updated on June 24, 2020

Comments

  • BolbazarMarme
    BolbazarMarme almost 4 years

    actually I've read some previous questions about this...

    this is the code that I use

    auto = (ListView)findViewById(R.id.auto);
    String[] projection = new String[] {Browser.BookmarkColumns._ID,Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL};
    
        String[] displayFields = new String[] {Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL};
    
        int[] displayViews = new int[] { R.id.text1,R.id.text2 };
    
        Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI,projection, null, null, null);
    
        //auto.setAdapter(new SimpleCursorAdapter(this, R.layout.mylist, cur,displayFields, displayViews));
    
        myAdapter apt = new myAdapter(this, R.layout.mylist, cur,displayFields, displayViews);
        auto.setAdapter(apt);
    

    and class myAdapter

    class myAdapter extends SimpleCursorAdapter{
    
        private Cursor c;
        private Context context;
    
    
        public myAdapter(Context context, int layout, Cursor c, String[] from,
                int[] to) {
            super(context, layout, c, from, to);
            // TODO Auto-generated constructor stub
            this.c = c;
            this.context = context;
            AutoList att = new AutoList();
            mListView = att.auto;
    
        }
    
        @Override
        public View getView(int pos, View inView, ViewGroup parent) {
               View vix = inView;
    
               if (vix == null) {
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    vix = inflater.inflate(R.layout.mylist, null);
               }
               this.c.moveToPosition(pos);      
    
               String title = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.TITLE));
    
               String cont = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.URL));
    
                   TextView text1 = (TextView) vix.findViewById(R.id.text1);
                   text1.setText(title);
                   TextView text2 = (TextView) vix.findViewById(R.id.text2);
                   text2.setText(cont);
                   Button butt = (Button) vix.findViewById(R.id.button);
                   butt.setOnClickListener(mButt);
                   return vix;
        }
    
        private OnClickListener mButt = new OnClickListener() {
            @Override
            public void onClick(View v) {
            final int position = mListView.getPositionForView((View) v.getParent());
                Log.v("BUTT", "Title clicked, row "+position);
            }
        };
    

    However, when I click the button, I still get a lot of errors like this:

    04-10 22:30:55.152: ERROR/AndroidRuntime(695): FATAL EXCEPTION: main
    04-10 22:30:55.152: ERROR/AndroidRuntime(695): java.lang.NullPointerException
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at             com.auto2.AutoList$myAdapter$1.onClick(AutoList.java:113)
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at   android.view.View.performClick(View.java:2408)
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at   android.view.View$PerformClick.run(View.java:8816)
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at android.os.Handler.handleCallback(Handler.java:587)
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at   android.os.Handler.dispatchMessage(Handler.java:92)
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at android.os.Looper.loop(Looper.java:123)
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at java.lang.reflect.Method.invoke(Method.java:521)
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    04-10 22:30:55.152: ERROR/AndroidRuntime(695):     at   dalvik.system.NativeStart.main(Native Method)
    

    That's it! I hope it won't be so difficult to be figured out!

    Thanks!

  • BolbazarMarme
    BolbazarMarme about 13 years
    in this tutorial stackoverflow.com/questions/1709166/… they said we need to add seTag and getTag for associated data that will be used. can you show me how to implement it?
  • Jim Blackler
    Jim Blackler about 13 years
    It should be a matter of adding an android:id=@+id/auto attribute to your ListView tag.
  • BolbazarMarme
    BolbazarMarme about 13 years
    sorry i'm kind of newbie for this, so do you mean i need to remove the listview id tag? or is there something else i need to do?
  • Houston
    Houston about 11 years
    Yip this is probably the fastest method...tags would just take up a tiny bit of memory...not sure what sort of impact that would cause in a list view with many items...

Related