Android:Button Onclick()在更新列表后不工作

Android:Button Onclick()在更新列表后不工作,android,button,onclick,Android,Button,Onclick,我的列表行的布局中有一个按钮。我需要根据按钮中的文本更改操作。这些文本可以是“还原”或“方向”。我已经创建了我自己的适配器类,如果按钮文本为“Revert”,它将进行更改。布局中提到的onclick函数将用于按钮文本“Directions” 适配器类中文本“Revert”的部分如下所示 if (btnChild.getText().toString().equals("Revert")) { btnChild.setOnClickListener(new View.OnClick

我的列表行的布局中有一个按钮。我需要根据按钮中的文本更改操作。这些文本可以是“还原”或“方向”。我已经创建了我自己的适配器类,如果按钮文本为“Revert”,它将进行更改。布局中提到的onclick函数将用于按钮文本“Directions”

适配器类中文本“Revert”的部分如下所示

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);
                }
            }
        });
    }
if(btnChild.getText().toString().equals(“Revert”)){
btnChild.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
btnChild.setText(“方向”);
checkbox.setChecked(false);
getItem(位置).setCompleted(错误);
如果(0是的,我明白你的意思

答案很简单。您将按钮的OnClick侦听器更改为一个全新的侦听器。 解决方案如下:

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);
            }
        }
    });
}
if(btnChild.getText().toString().equals(“Revert”)){
btnChild.setText(“方向”);
checkbox.setChecked(false);
getItem(位置).setCompleted(错误);
如果(0是的,我明白你的意思

答案很简单。您将按钮的OnClick侦听器更改为一个全新的侦听器。 解决方案如下:

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);
            }
        }
    });
}
if(btnChild.getText().toString().equals(“Revert”)){
btnChild.setText(“方向”);
checkbox.setChecked(false);
getItem(位置).setCompleted(错误);
如果(0这真是一团糟:)

首先,您不需要使用标签或任何其他可视属性来控制应用程序。设计可以更改,语言可以更改,然后您必须在逻辑中重写应用程序

其次,您不应该每次事件发生时都创建新的事件侦听器。这非常耗费资源

您应该做的是为项目创建一个neet小类:

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
   }
}
创建以下侦听器的数组:

MyListener[] listeners = new MyListener[numberOfItems];
当您想从适配器获取项目时:

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;
}这真是一团糟:)

首先,您不需要使用标签或任何其他可视属性来控制应用程序。设计可以更改,语言可以更改,然后您必须在逻辑中重写应用程序

其次,您不应该每次事件发生时都创建新的事件侦听器。这非常耗费资源

您应该做的是为项目创建一个neet小类:

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
   }
}
创建以下侦听器的数组:

MyListener[] listeners = new MyListener[numberOfItems];
当您想从适配器获取项目时:

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;
}