Poly Bug
Poly Bug

Reputation: 1542

Android ListView how to disable multi click

I have a listview that contains a textview and a delete imagebutton for each row in the list. I populated this listview using a custom adapter and overriding the getView method. However, I also assigned onClick listeners to each textview to open up a dialog for editing the text (as well as the imagebutton for deleting that row in the listview.)

My problem is when the user "accidentally" clicks on two rows using two fingers at the same time, it pops up two dialogs, one is hidden behind the other. The user can simply dismiss both, but I'd like to avoid this glitch by not making it happen at all.

Here's the code from getView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ListItem li = getItem(position);    
    ViewHolder viewHolder;
    if(convertView == null) {
        viewHolder = new ViewHolder(); 
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.pref_sqlite_table_row, parent, false);
        viewHolder.imageButton = (ImageButton) convertView.findViewById(R.id.pref_remove_button);
        viewHolder.textView = (TextView) convertView.findViewById(R.id.pref_text_view);
        //we need to update adapter once we finish with editing
        convertView.setTag(viewHolder);
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.imageButton.setTag(li.getId());
    // imageButton onClick to remove row

    viewHolder.textView.setText(li.getData());
    viewHolder.textView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView tv = (TextView) v;
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            View vw = ((Activity) mContext).getLayoutInflater().inflate(R.layout.pref_sqlite_table_dialog, null);
            mNewChangeDialogEditText = (EditText) vw.findViewById(R.id.pref_edit_text);
            mNewChangeDialogEditText.setText(tv.getText());

            ImageButton ib = (ImageButton) vw.findViewById(R.id.pref_accept_button);
            ib.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // update data
                    mNewChangeDialog.dismiss();
                }
            });
            builder.setView(vw);
            mNewChangeDialog = builder.create();
            mNewChangeDialog.show();
        }
    });
    return convertView;
}

Upvotes: 1

Views: 1129

Answers (1)

user370305
user370305

Reputation: 109247

Simple, First of all don't create AlertDialog object in getView(). Create It in Constructor of your custom adapter.

Now Just assign values of Textview to EditText of AlertDialog in getView() for that use reference of EditText of AlertDialog as per Class level.

Also put all the stuff of AlertDialog in Constructor of Custom Adapter only even if ib.setOnClickListener().

Now in ib.setOnClickListener() just show Dialog if its not Visible using isVisible() or isShown() method and dismiss() if it already visible.

(I don't remember methods).

Upvotes: 1

Related Questions