简体   繁体   中英

Want to do something to view in item that got clicked in list view

I have two buttons in each item that are set to invisible. I want, that when the user clicks on an item, the buttons only in that item turn to visible.

Im using a custom adapter for my list view...

public class LocationAdapter extends BaseAdapter{
    String [] n;
    Context context;
    String[] a;
    int bint = View.INVISIBLE;

    private static LayoutInflater inflater=null;
    public LocationAdapter(MainActivity mainActivity, String[] names, String[] addresses, int bint) {
        // TODO Auto-generated constructor stub
        this.bint = bint;
        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;
        Button b1;
        Button b2;
    }

    @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.b1 = (Button) rowView.findViewById(R.id.Edit);
        holder.b2 = (Button) rowView.findViewById(R.id.Delete);
        holder.b1.setVisibility(bint);
        holder.b2.setVisibility(bint);
        holder.name.setText(n[position]);
        holder.address.setText(a[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;
    }
}

Can anyone please give me ideas on how i need to do this?

You need to set an OnItemClickListener on your ListView and change the visibility of the Buttons inside onItemClick() .

Also note that your current implementation of LocationAdapter does not check if(convertView == null) and it does not set the ViewHoler as the tag of the row View .

you only can set onclicklistener for view objects and its childs....so either you implementate your own API where you have a method that does the same as the standard onclicklistener but you can set it on every object that you want...would be very interesting imho...

or you use views like textviews etc and then set an onclicklistener on them!!!

here is what you can try Add onclicklsitener to buttom instead of entire view. Inside buttons xml to which you want apply click listener add following two lines

android:focusable="false"
android:focusableInTouchMode="false"

now apply listener to button as follows

    holder.button.setOnClickListener(new OnClickListener()
      {
          @Override
          public void onClick(View v)
          {
              //set visibility of items here
              holder.buttonedit.setVisibility(View.GONE);
              holder.buttonedit.setVisibility(View.VISIBLE);

          }
      });

Inside getview method declare holder final

final Holder holder=new Holder();

and you are done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM