如何在我的适配器生成的按钮上设置单击侦听器?

时间:2011-09-14 19:49:25

标签: android user-interface adapter

这是我的简单适配器:

public class MainAdapter extends BaseAdapter {

    private Context mContext;

    private Integer[] mText = {
            R.string.main_team,
            R.string.main_league,
            R.string.main_economy,
            R.string.main_arena,
            R.string.main_staff,
            R.string.main_team_up
    };

    public MainAdapter(Context c) {
        mContext = c;
    }

    @Override
    public int getCount() {
        return mText.length;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Button buttonView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            buttonView = new Button(mContext);
            buttonView.setLayoutParams(new GridView.LayoutParams(85, 45));
            //imageView.setScaleType(Button.ScaleType.CENTER_CROP);
            buttonView.setPadding(4, 4, 4, 4);
        } else {
            buttonView = (Button) convertView;
        }

        buttonView.setText(mText[position]);
        return buttonView;
    }

}

每个按钮都必须启动自己的意图。 我正在考虑使用基于位置的开关盒,但它似乎真的是“愚蠢”的解决方案。

2 个答案:

答案 0 :(得分:1)

我会创建一个新对象,例如。 ActivityLaunchButtonData存储按钮Text和按钮Intent。

然后我会有一个ActivityLaunchButtonData对象数组。

然后在getView方法中,我有类似的东西:

ActivityLaunchButtonData activityLaunchButtonData = activityLaunchButtonDataArray[position];
buttonView.setText(activityLaunchButtonData.getText());
buttonView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = activityLaunchButtonData.getIntent();
            startActivity(intent);
        }
    });

答案 1 :(得分:0)

如果您知道,在您的适配器中触发的意图和要放入的数据,您可以尝试这样的事情:

public class MainAdapter extends BaseAdapter implements OnClickListener {

    private Context mContext;
    private Activity mActivity;

    private Integer[] mText = {
        R.string.main_team,
        R.string.main_league,
        R.string.main_economy,
        R.string.main_arena,
        R.string.main_staff,
        R.string.main_team_up
    };

    public MainAdapter(Context c,Activity a) {
        mContext = c;
        mActivity = a;
    }

    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Button buttonView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            buttonView = new Button(mContext);
            buttonView.setLayoutParams(new GridView.LayoutParams(85, 45));
            //imageView.setScaleType(Button.ScaleType.CENTER_CROP);
            buttonView.setPadding(4, 4, 4, 4);
        } else {
            buttonView = (Button) convertView;
        }

        buttonView.setText(mText[position]);
        // For exemple a String, but setTag takes an object
        buttonView.setTag("dataToSendThrewTheIntent");
        buttonView.setOnClickListener(this);
        return buttonView;
    }

    @Override
    public void onClick(View v) {
        String dataToSendThrewTheIntent = (String) v.getTag();
        Intent i = new Intent(mContext, DestinationActivity.class);
        i.putExtra("key", dataToSendThrewTheIntent);
        mActivity.startActivity(i);
    }
}

此方法可避免多个onClick侦听器。