androidlistviewcheckedtextview

listview is populated with same value multiple times during multiple button clicks


I have got a xml file with listview in a file list.xml

    <LinearLayout>

<Button
    android:id="@+id/countryfilter"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:text="Load Country data"/>


<Button
    android:id="@+id/domain_filter"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:text="Load Domain data"/>

    <ListView
                android:id="@+id/listContents"
                android:layout_width="wrap_content"
                android:visibility="gone"
                android:layout_height="wrap_content">
            </ListView>
     </LinearLayout>

Following is the code that I have within row.xml

 <CheckedTextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some text here"
        android:textSize="18sp"
        android:gravity="center_vertical"
        android:clickable="true"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:focusable="true"
        android:checked="false"
        android:onClick="toggle"/>

During run time, the listview will be made made visible and its data will be set.

Here Im setting onClickListener for load country list button. Countrylist item has got list of some countries

countryListBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            lv.setVisibility(View.VISIBLE);
            initCountryList();

            customAdapter = new CustomAdapter(
                    getApplicationContext(), R.layout.filters, android.R.id.text1, countryList
            );
            lv.setAdapter(customAdapter);
        }
    });

Here Im setting onClickListener for load domain list button. domainList contains list of domains

domainListBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            lv.setVisibility(View.VISIBLE);
            initDomainList();
            domainAdapter = new CustomAdapter(getApplicationContext(), R.layout.filters, android.R.id.text1, domainList
            );
            lv.setAdapter(domainAdapter);
        }
    });

The problem is that,

listview is populated with values each time when buttons are clicked. so the listview has got same values that are being displayed multiple times.


Solution

  • I recommend you to initialize and assign the adapter in onCreate() rather initializing multiple times on button click. As the model is same, no need to create two separate lists and adapters.

    In onCreate()

    // Initialize list and assign it to adapter
    dataList = new ArrayList<>();
    adapter = new CustomAdapter(getApplicationContext(), R.layout.filters, android.R.id.text1, dataList
                );
    lv.setAdapter(adapter);
    

    And coming to button click listeners,just clear the list and then add country or domain list and finally call adatper.notifyDataSetChanged().

    countryListBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lv.setVisibility(View.VISIBLE);
                // In this method, clear the list first then add the countries list
                initCountryList();
                adapter.notifyDataSetChanged();
            }
        });
    
        domainListBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lv.setVisibility(View.VISIBLE);
                // Same as countries list
                initDomainList();
                adapter.notifyDataSetChanged();
            }
        });
    

    Hope this will help you out.