siraj
siraj

Reputation: 461

Android: Button Onclick() not working after updating list

There is a button in the layout of the rows of my list.I need to change the action based on the text in the button.These text can be "Revert" or "Directions". I have created my own adapter class which make changes if the button text is "Revert".The onclick function mentioned in the layout will be working for the button text "Directions".

//This  is my Onclick() function mentioned in the layout  
public void changecolour(View v) {
    LinearLayout vwParentRow = (LinearLayout) v.getParent();
    final Button btnChild = (Button) vwParentRow.getChildAt(2);
    if (btnChild.getText().toString().equals("Directions")) {
                Intent directiosin = new Intent(getApplicationContext(),
                        Directions.class);
                startActivity(directiosin);
    }

The portion in the adapter class for the text "Revert" shown below.

if (btnChild.getText().toString().equals("Revert")) {
        btnChild.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                btnChild.setText("Directions");
                checkbox.setChecked(false);
                getItem(position).setCompleted(false);
                if (0 <= list.get(position).getStatus()
                        && list.get(position).getStatus() < 5) {
                    text.setTextColor(Color.BLUE);
                } else if (5 <= list.get(position).getStatus()
                        && list.get(position).getStatus() < 10) {
                    text.setTextColor(Color.parseColor("#DD7500"));
                } else {
                    text.setTextColor(Color.RED);
                }
            }
        });
    }

When I click on the "Directions" button The intent is successfully called .When I click on "Revert" button,button text is changed to "Directions" .But when this button is clicked the "directionsIn" intent is not called.

Upvotes: 0

Views: 626

Answers (2)

This is realy messed up :)

First of all, you don't use the label or any other visual property to control your application. Design can be changed, language can be changed, and than you'll have to rewrite applicatoin logic.

Secondly, you shouldn't create new event listeners every time an event occurs. It's very resource consuming.

What you should do, is to create a neet little class for the items:

private class MyListener implements OnClickListener {
   boolean revert = true;
   int id;
   Button button;

   private MyListener(int id) {
      this.id = id;
   }

   public void OnClick(View v) {
      // Do stuff that should be done
      if (revert) {
         // Calls the method for revert, you have the id of the selected item
      } else {
         // Calls the method for directions, you have the id of the selected item
      }
      // TODO Change label of the button
      // TODO Change state of listener
   }
}

Create an array of these listeners:

MyListener[] listeners = new MyListener[numberOfItems];

And when you want to get an item from the adaptor:

public View getView(int position, View convertView, ViewGroup parent) {
   View view = convertView;
   if (view == null) {
      // TODO If View doesn't exist, create a new one
   }
   Button button = (Button)view.findViewById(R.id.theButtonId);
   if (listeners[position] == null) {
      listeners[position] = new MyListener(position);
   }
   button.setText(listeners[position].revert?"Revert":"Directions");
   button.setOnClickListener(listeners[position]);
   listeners[position].button = button;

}

Upvotes: 1

Alone89
Alone89

Reputation: 307

Yes I see your point.

The answer is easy. You changed the button's onClickListener to a completely new one Listener. The solution is something like this :

if (btnChild.getText().toString().equals("Revert")) {

            btnChild.setText("Directions");
            checkbox.setChecked(false);
            getItem(position).setCompleted(false);
            if (0 <= list.get(position).getStatus()
                    && list.get(position).getStatus() < 5) {
                text.setTextColor(Color.BLUE);
            } else if (5 <= list.get(position).getStatus()
                    && list.get(position).getStatus() < 10) {
                text.setTextColor(Color.parseColor("#DD7500"));
            } else {
                text.setTextColor(Color.RED);
            }
        }
    });
}

Upvotes: 2

Related Questions