Android:按下listview项目中的按钮

时间:2012-11-06 09:46:10

标签: android listview android-listview adapter

我有一个自定义列表视图适配器,其中包含两个按钮,问题是难以点击按钮并且它一直失去焦点,或者您必须多次触摸它才能识别出点击。

@Override
public View getView(int position, View view, final ViewGroup parent) {

    DataEntity data = entityList.get(position);

    view = inflater.inflate(R.layout.new_mission_row, parent, false);

// inflate other views


    Button playButton = (Button) view.findViewById(R.id.buttonPlay);
    playButton.setBackgroundResource(R.drawable.blue_button);
    playButton.setTextColor(Color.WHITE);
    MyClickListener listener = new MyClickListener(context,entity);
    playButton.setOnClickListener(listener);
    playButton = (Button) view.findViewById(R.id.buttonMap);
    playButton.setBackgroundResource(R.drawable.blue_button);
    playButton.setTextColor(Color.WHITE);
    playButton.setOnClickListener(listener);
    return view;
}

2 个答案:

答案 0 :(得分:1)

见下文。

@Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageLoader = new SISIImageLoader(MY_ACTIVITY.this);
            v = vi.inflate(R.layout.game_display_row, null);
            ViewHolder holder = new ViewHolder();

            holder.deleteGameBtn = (Button) v.findViewById(R.id.deleteGameBtn);


            v.setTag(holder);
        }

        final Gamedata o = items.get(position);

        ViewHolder holder = (ViewHolder) v.getTag();


        if (o != null) {

            Button deleteGameBtn = (Button) v.findViewById(R.id.deleteGameBtn);

            holder.deleteGameBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                        // do what you want to do on click 

                    }


            });

        }
        return v;
    }

希望它能解决你的问题。

答案 1 :(得分:1)

检查您的代码一次。您尚未为buttonmap创建不同的按钮对象。

playButton = (Button) view.findViewById(R.id.buttonMap);

您使用的是与buttonPlay

创建的按钮对象相同的按钮对象

此外,当您填充ListView时,您应该使用ViewHolder pattern有效地填充列表视图。