简体   繁体   中英

onItemLongClick won't update a view item in listview

Good day!

I would like to know the reason on why onItemLongClick won't update a view item in listview but when i used onItemClick it work as expected this is the snippet of my code. My apologies i forgot to add return statement in that snippet but the problem still unresolved.

private void updateView(int position) {
    View v = listView.getChildAt(position -
            listView.getFirstVisiblePosition());

    ImageView tempImgView = (ImageView) v.findViewById(R.id.avatarIcon);
    if (listView.isItemChecked(position)) {
        tempImgView .setImageResource(R.drawable.ic_check_24dp);
    } else {
        tempImgView .setImageResource(R.drawable.ic_uncheck_24dp);
    }
}


@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
        long id) {
        updateView(position);
        return true;
}

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int position,
        long id) {
    updateView(position);
    return true;
}

You have to return true, so android will know that your click has been consumed.

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int position,
        long id) {
    updateView(position);
   return true;
}

You will have to return true value of onItemLongClick listener, try this :-

    @Override
     public boolean onItemLongClick(AdapterView<?> arg0, View view, int   position,
        long id) {
     updateView(position);
    return true;
     }

Hope this will help you . :)

After instantiate your item view, set:

yourItemView.setLongClickable(true);
yourItemView.setOnLongClickListener(this);

And remember to add the long-click event return:

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int position,
        long id) {
    updateView(position);
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM