0

我用适配器类创建了列表视图,在列表项中,我给出了三个按钮。我还需要更改该按钮和其他按钮的背景图像。我有 5 个列表项,每个项都有自己的按钮。

现在,当我单击第一个、第二个到第五个列表项的按钮时,最后一个图像的背景只会发生变化。如果我单击相应的按钮取决于逻辑,每个列表项的按钮的背景图像都应该改变。它只发生在最后一个列表项..

如果有人知道如何做到这一点,请帮助我..

代码是:

public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    init();

}

private void init() {

    LayoutInflater inflater1 = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);          
    View view = inflater1.inflate(R.layout.datelabel, null);

    setListAdapter(new EventListAdapter(this));     

    ListView listView = getListView();
    listView.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            Toast.makeText(MainActivity.this, "tapped on position " + position, Toast.LENGTH_SHORT).show();

            return false;
        }
    });


}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) 
{  
    ((EventListAdapter)getListAdapter()).toggle(position);

}
private class EventListAdapter extends BaseAdapter {
    private static final int VISIBLE = 0;


    private static final int GONE = 8;


    private TextView textViewForDateHeader;
    private TextView textViewTitle;
    private TextView textViewDialogue;

    private TextView textViewHeader;




    private ImageButton buttonForCheckMark;


    private ImageButton buttonForDelete;


    private View buttonForRemainder;



    public EventListAdapter(Context context)
    {
        mContext = context;
    }
    public int getCount() {
        return mTitles.length;
    }


    public Object getItem(int position) {
        return position;
    }


    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View myView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);           
        myView = inflater.inflate(R.layout.activity_main, null);


        textViewTitle = (TextView) myView.findViewById(R.id.textViewTitle);
        textViewTitle.setText(mTitles[position]);

        textViewDialogue = (TextView) myView.findViewById(R.id.textViewDialog);
        textViewDialogue.setText(mDialogue[position]);
        textViewDialogue.setVisibility(mExpanded[position] ? VISIBLE : GONE);

        buttonForCheckMark = (ImageButton) myView.findViewById(R.id.buttonForCheckMark);
        buttonForCheckMark.setVisibility(mExpanded[position] ? VISIBLE : GONE);

        buttonForDelete = (ImageButton) myView.findViewById(R.id.buttonForDelete);
        buttonForDelete.setVisibility(mExpanded[position] ? VISIBLE : GONE);

        buttonForRemainder = (ImageButton) myView.findViewById(R.id.buttonForRemainder);
        buttonForRemainder.setVisibility(mExpanded[position] ? VISIBLE : GONE);

        buttonForRemainder.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, "tapped on remainder", Toast.LENGTH_SHORT).show();

            }
        });
        buttonForCheckMark.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "tapped on checkMark", Toast.LENGTH_SHORT).show();
                buttonForCheckMark.setBackgroundResource(R.drawable.ic_launcher);
                buttonForDelete.setBackgroundResource(R.drawable.ic_navigation_cancel);
                buttonForCheckMark.setClickable(false);
                buttonForDelete.setClickable(true);
            }
        });
        buttonForDelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "tapped on delete", Toast.LENGTH_SHORT).show();
                buttonForCheckMark.setBackgroundResource(R.drawable.ic_navigation_accept);
                buttonForCheckMark.setClickable(true);
                buttonForDelete.setBackgroundResource(R.drawable.ic_drawer);
                buttonForDelete.setClickable(false);
            }
        });


        return myView;
    }


    public void toggle(int position) {


        mExpanded[position] = !mExpanded[position];
        notifyDataSetChanged();


    }

    /**
     * Remember our context so we can use it when constructing views.
     */
    private Context mContext;


    /**
     * Our data, part 1.
     */
    //      private ImageButton[] mButtons = {
    //
    //              "R.drawable.remainder",
    //              
    //      };
    private String[] mHeader = 
        {
            "12 Jan, 2013",   
            "13 Feb, 2013",
            "31 Mar, 2013",       
            "15 Aug, 2013",
            "7 Sep, 2013"

        };


    /**
     * Our data, part 1.
     */
    private String[] mTitles = 
        {
            "Event 1",   
            "Event 2",
            "Event 3",       
            "Event 4",
            "Event 5"

        };

    /**
     * Our data, part 2.
     */
    private String[] mDialogue = 
        {
            "wuszuogwfuieffufuhuysugdueljwihadghgxdhgyhghsdgyigwuweyuqaGDHGYHGHGAdhgyhigxgxgeuyehu.",
            "dgusduugyujguegytgujgdugwjhiuyg7wtqUYGYYgyijyiufufjguhgdugfhgfhgfgfhgfhghfghifgyi,dgwsdtgyfytfiuwt,",
            "rtygygghtudggyjhgujtugdhhguyuaUUUUDJYUIDHUJHDIIDUJDHDUJHDIDIOUYhujtdugyhdgg",
            "gjhuwjsgudggdudgjqhasdgdhgjdhushjaguhguwegagsdgygydgfgdcgycg",
            "fhdgyhdfhfgdyhhwsddgyuduuufguugwugdfgugdgooduiuduiuduuduiuiuidudiiwdiou"


        };

    /**
     * Our data, part 3.
     */
    private boolean[] mExpanded = 
        {
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            false   
        };


}

}
4

2 回答 2

1

而不是在适配器中拥有私有 iVars ..您应该将它们实例化为方法final内部getView()的变量..当前在侦听器中设置新图像...但变量仅指向最后创建的按钮的地址。

将您的代码更新为这样

private class EventListAdapter extends BaseAdapter {
    /// other things

    //private TextView textViewForDateHeader; // remove such private vars

    .. other methods

    public View getView(int position, View myView, ViewGroup parent) {
          //other code

      final ImageButton  buttonForCheckMark = (ImageButton) myView.findViewById(R.id.buttonForCheckMark);

          buttonForCheckMark.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                buttonForCheckMark.setBackgroundResource(R.drawable.ic_launcher);
                //now the buttonForCheckMark refers to the imageview your instantiated above .. not the private var
               //other code
            }
        });

编辑

对于高亮坚持.. 在适配器内添加一个布尔数组。

 private boolean highlighted[];

在里面EventListAdapter()也将它初始化为 mTitles 大小

  public EventListAdapter(Context context)
    {
            highlighted = new boolean[mTitles.length];
            mContext = context;
    }

现在在 onListener .. 检查 highlight[clickPos] == true .. 禁用它,如果它是 false .. 将其设置为 true;您还可以使用此值将高亮保留在getView()

于 2013-10-18T13:16:33.807 回答
0

getView在您的方法中移动以下声明。

TextView textViewForDateHeader;
TextView textViewTitle;
TextView textViewDialogue;
TextView textViewHeader;
ImageButton buttonForCheckMark;
ImageButton buttonForDelete;
View buttonForRemainder;
于 2013-10-18T13:16:50.773 回答