Izak
Izak

Reputation: 991

How to know in which item a button was clicked

So far i have a list view with a custom adapter, and each item in list has a button. Im really confused; Im trying to do the following:

When user clicks on button(a delete button) in item in the list, i want to know in which item button was clicked so i can know which item to delete-how do i implement this? Ive seen something about setting tags, but Im still very lost.

I have also tried to reach the button from the list layout from my main activity, and cannot reference it.

please can you give me a detailed description on how to do what i want to do thanks.

ADDED adapter code:

public class LocationAdapter extends BaseAdapter{
String [] n;
Context context;
String[] a;

private static LayoutInflater inflater=null;
public LocationAdapter(MainActivity mainActivity, String[] names, String[] addresses) {
    // TODO Auto-generated constructor stub
    n=names;
    context=mainActivity;
    a=addresses;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return n.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView name;
    TextView address;
    ImageButton ib;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.rowlayout2, null);
    holder.name =(TextView) rowView.findViewById(R.id.EditTextName);
    holder.address =(TextView) rowView.findViewById(R.id.EditTextAddress);
    holder.ib = (ImageButton) rowView.findViewById(R.id.Delete);
    holder.name.setText(n[position]);
    holder.address.setText(a[position]);
    holder.ib.setTag(convertView);
    rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+n[position], Toast.LENGTH_LONG).show();
        }

    });
    return rowView;
}

}

Upvotes: 0

Views: 90

Answers (5)

inmyth
inmyth

Reputation: 9050

You should not use Array. Array has fixed length so you cannot exactly remove its element. You need to use ArrayList in place of Array and implement ViewHolder pattern correctly:

ArrayList<Item> items = new ArrayList<>();
//merge both the content of a and n into one object "Item"
//and use ArrayList

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

      Holder holder;
      if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.rowlayout2, parent, false);
        holder = new Holder(convertView);
        convertView.setTag(holder);
       } else {
        holder = (Holder) convertView.getTag();
       }

       holder.name.setText(getItem(position).name);
       ...
       holder.root.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            items.remove(position);
            notifyDataSetChanged();
        }
       });
     return convertView;
}

If you want to set clickable on the whole row with then you need to put the root element inside the ViewHolder.

class Holder{
   TextView name, address, ...;
   LinearLayout root; // or any Layout which is the root container of your row
}

Upvotes: 0

Duc Nguyen
Duc Nguyen

Reputation: 87

public class SeekBarAdapter {private SeekBarListener mListener;
private ClickListener mListenerClick;
public interface SeekBarListener {

    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser, int positionInList);

    public void onStartTrackingTouch(SeekBar seekBar, int positionInList);

    public void onStopTrackingTouch(SeekBar seekBar, int positionInList);
}

public interface ClickListener {
    public void onClick(View v, int positionInList);

}

public listAdapter getAdapter(Context context, ItemPhotoEffect itemPhotoEff) {
    return new listAdapter(context, itemPhotoEff);
}

public void setSeekBarListener(SeekBarListener listener) {
    mListener = listener;
}

public void setClickListener(ClickListener listener) {
    mListenerClick = listener;
}

public class listAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private onSeekbarChange mSeekListener;
    private onClickChange mImgClickListener;
    private ArrayList<Bitmap> itemsList;
    private Bitmap imgIcon;
    private ArrayList<Integer> positionProgress;
    private ArrayList<ColorEffectItem> colorItem;

    public listAdapter(Context context, ItemPhotoEffect itemPhotoEff) {
        mInflater = LayoutInflater.from(context);
        if (mSeekListener == null) {
            mSeekListener = new onSeekbarChange();
        }
        if (mImgClickListener == null) {
            mImgClickListener = new onClickChange();
        }
        this.itemsList = itemPhotoEff.getSeekBarItem().getIconSeekBar();
        this.positionProgress = itemPhotoEff.getSeekBarItem()
                .getPositionProgress();

        // if (itemPhotoEff.getSeekBarItem().getColorItem() != null) {
        this.colorItem = itemPhotoEff.getSeekBarItem().getColorItem();
        // }
    }

    @Override
    public int getCount() {
        if (itemsList != null) {
            return itemsList.size();

        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        // if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(R.layout.seekbar_adapter_layout,
                null);
        holder.img = (ImageView) convertView.findViewById(R.id.imageView1);
        holder.seekbar = (SeekBar) convertView.findViewById(R.id.seekBar1);
        holder.colorbutton = (ImageView) convertView
                .findViewById(R.id.colorImage);
        holder.ln_img = (LinearLayout) convertView
                .findViewById(R.id.ln_colorimg);
        convertView.setTag(holder);
        // } else {
        // holder = (ViewHolder) convertView.getTag();
        // }
        holder.img.setImageBitmap(itemsList.get(position));

        if (positionProgress.get(position) != null) {
            holder.seekbar.setVisibility(View.VISIBLE);
            holder.ln_img.setVisibility(View.GONE);
            holder.seekbar.setProgress(positionProgress.get(position));
        } else {
            holder.ln_img.setVisibility(View.VISIBLE);
            holder.seekbar.setVisibility(View.GONE);
            holder.colorbutton.setBackgroundColor(Color.rgb(this.colorItem
                    .get(position).getRcolor(), this.colorItem
                    .get(position).getGcolor(), this.colorItem
                    .get(position).getBcolor()));
        }
        holder.seekbar.setOnSeekBarChangeListener(mSeekListener);
        holder.ln_img.setOnClickListener(mImgClickListener);
        holder.seekbar.setTag(position);
        holder.ln_img.setTag(position);
        return convertView;

    }
}

static class ViewHolder {
    ImageView img;
    SeekBar seekbar;
    ImageView colorbutton;
    LinearLayout ln_img;
}// This is class you need to know which button has been click
public class onClickChange implements OnClickListener {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int position = (Integer) v.getTag();
        if (mListenerClick != null) {
            mListenerClick.onClick(v, position);
        }
    }

}

public class onSeekbarChange implements OnSeekBarChangeListener {

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        int position = (Integer) seekBar.getTag();

        if (mListener != null) {
            mListener.onProgressChanged(seekBar, progress, fromUser,
                    position);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        int position = (Integer) seekBar.getTag();
        if (mListener != null) {
            mListener.onStartTrackingTouch(seekBar, position);
        }
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int position = (Integer) seekBar.getTag();
        if (mListener != null) {
            mListener.onStopTrackingTouch(seekBar, position);
        }
    }
}}

You can use mycode. Implements interface OnclickListener in your adapter. And then implement in activity:
+ PositionInList use if you have than 1 button in row.

adapter.setClickListener(new ClickListener() {
        @Override
        public void onClick(View v, int positionInList) {
            // TODO Auto-generated method stub
            Log.i("", "Click on position in list" + positionInList);
        }
    });

Upvotes: 0

Daniel Nugent
Daniel Nugent

Reputation: 43322

Implement an OnClickListener for your delete button.

When the delete button is clicked, remove the row in the data source at position, and then call notifyDataSetChanged()

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.rowlayout2, null);
    holder.name =(TextView) rowView.findViewById(R.id.EditTextName);
    holder.address =(TextView) rowView.findViewById(R.id.EditTextAddress);
    holder.ib = (ImageButton) rowView.findViewById(R.id.Delete);
    holder.name.setText(n[position]);
    holder.address.setText(a[position]);
    holder.ib.setTag(convertView);

    //Add this for on-click of delete button
    holder.ib.setOnClickListener(new OnClickListener(){
         //Delete the row in your data source specified at position

    });

    rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+n[position], Toast.LENGTH_LONG).show();
        }

    });
    return rowView;
}

Upvotes: 2

Blaze Tama
Blaze Tama

Reputation: 10948

You need to add the onClick in the getView.

Example :

http://jmsliu.com/2444/click-button-in-listview-and-get-item-position.html

Theres a lot of tutorials for this out there.

Upvotes: 0

Sandeep Singh
Sandeep Singh

Reputation: 1127

Use this inside your getView() method of adapter.

    yourButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
               System.out.println("button : "+position+ " clicked");
               // Do you code here.
        }
    });

Upvotes: 1

Related Questions