0

I am developing an Android Application. In this app I am making a list of views using code given below. In each item of list there is delete button with visibility "Gone". Now there is another button Edit outside the list, on click of edit button I have to show delete button on each item of list. but using this code delete button shows only in the last item. Please help me to solve the problem. Thanks.

dynamicView();


edit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        //  lay.removeAllViews();
        addnew.setVisibility(View.INVISIBLE);

        btn_red.setVisibility(View.VISIBLE);
        edit.setText("Done");

        }
    });

public void dynamicView() {

     LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        for (int i = 0; i < templates.length; i++) {
            final View customView = linflater.inflate(R.layout.order_template_item,
                    null);
            btn_red=(ImageView)customView.findViewById(R.id.btn_negative);
            btn_drag=(ImageView)customView.findViewById(R.id.button_drag);
            btn_delete=(ImageView)customView.findViewById(R.id.button_delete);
           final ImageView image = (ImageView)customView.findViewById(R.id.arrow);
           final TextView text = (TextView)customView.findViewById(R.id.date);
           final TextView sku = (TextView)customView.findViewById(R.id.time);
           final TextView price = (TextView)customView.findViewById(R.id.last);
           final TextView names =(TextView)customView.findViewById(R.id.name);

           image.setId(i);
           text.setId(i);
           sku.setId(i);
           price.setId(i);
           names.setId(i);
           btn_red.setId(i);
           btn_red.setTag(i);
           btn_delete.setId(i);
           btn_drag.setId(i);

          names.setText(templates[i]);

           btn_red.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                btn_delete.setVisibility(View.VISIBLE);
                TranslateAnimation anim = new TranslateAnimation(100,0 , 0, 0);
                anim.setInterpolator(new BounceInterpolator());
                anim.setDuration(1000);
                btn_delete.setAnimation(anim);
            }
        });
           btn_delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            lay.removeView(customView); 

            }
        });


           lay.addView(customView);
     }
Balvinder Singh
  • 580
  • 6
  • 19

2 Answers2

0

You're holding reference only to the last one btn_red.

You can do something like this.

List<Button> buttons = new LinkedList<Button> ();

Then in your loop after findViewById on btn_red

buttons.add(btn_red);

And finally in your onClickListener

for (Button button: buttons) {
    button.setVisibility(View.VISIBLE);
}
cliffroot
  • 1,839
  • 17
  • 27
0

Do somethink like below to make visible all the button added to list items-

edit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        addnew.setVisibility(View.INVISIBLE);


        for (int i = 0; i < templates.length; i++) {

          int id  = btn_red.getId(i);
          id.setVisibility(View.VISIBLE);
       }
       edit.setText("Done");

        }
    });