Badr Ghatasheh
Badr Ghatasheh

Reputation: 978

Android: Pressing a button in a listview item

I have a custom listview adapter which contains two buttons, the problem is it's hard to click on the button and it keeps losing focus, or you have to touch it multiple times before it recognizes the click.

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

Upvotes: 2

Views: 826

Answers (2)

Dharmendra
Dharmendra

Reputation: 33996

Check your code once. You have not created a different button object for buttonmap.

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

Here you are using the same button object as you have created for buttonPlay

Also when your are populating the ListView then you should use the ViewHolder pattern to populate the listview efficiently.

Upvotes: 1

Shreyash Mahajan
Shreyash Mahajan

Reputation: 23596

See below one.

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

Hope it will solve your problem.

Upvotes: 1

Related Questions