This is a listView question.

Asked 2 years ago, Updated 2 years ago, 49 views

The Listview item has a radio button image. When Listview first appears, the radio button is checked in one item. After that, if you press the radio button of the item except for the first selected radio button, the first selected radio button disappears and I want to check the newly selected radio button. Here's the code.

publc void setRbImage(int location, int resId) {
    Drawable dr = getResoures().getDrawable(resId);
    mListData.get(location).mSelect = drawable;
    listAdapter.notifyDataSetChanged();
}

public void onItemClick(Adapter<?> parent, View view, int position, long id) {
    // Remove the first selected radio image
    if(!selLocation.equals("")) {
        listAdapter.setRbImage(selLocation, R.drawable.radio_normal);
        selLocation = "";
    }

    if(mView != null) {
        ((ImageView) mView).setImageResource(R.drawable.radio_normal);
    }
    // Current Click Duan View Circle Check
    View clickView = view.findViewById(R.id.mbl_radio_check);
    ((ImageView) clickView).setImageResource(R.drawable.radio_checked);

    // Save the currently clicked view
    mView = clickView;
}

If you use notifyDataSetChanged() in the setRbImage method, the first selected radio button disappears when you click the item, but the newly clicked radio button does not appear. If notifyDataSetChanged() is not used, the first selection is not lost when you click the item, and the newly clicked radio button image is displayed, drawing two.

It may be uncomfortable to play one by one, but please help me~ Thank you

listview notifydatasetchanged

2022-09-21 19:33

1 Answers

When notifyDataSetChanged() is called, the first selected radio button disappears, but the newly clicked radio button does not appear:

Invoking notifyDataSetChanged() in the list view will redraw the view through the adapter. The first selected radio button is drawn as intended even if the view is redrawn (with mSelect=R.drawable.radio_normal) because the model was changed via the setRbImage() function.

The reason why the newly clicked radio button is not drawn is because the model does not have the information 'Show me the radio button image'. In other words, the code you uploaded only modified the view, so if you call notifyDataSetChanged(), the view is initialized and not drawn.

mListData.get(location).Similar to mSelect, implement it by storing whether or not the current item is selected in the model (like the first radio button selected).


2022-09-21 19:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.