如何在单击ListView中的按钮时取消选中所有List项

时间:2015-01-12 08:58:12

标签: android listview checkbox android-listview

我有一个应用程序,其中我在ListView顶部有重置按钮,并且在单击重置视图时,Listview中检查的所有项目都应该取消选中。

我已尝试过以下链接的代码
http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php

这些片段是:

public class PlanetsActivity extends Activity {

    private ListView mainListView ;
    private Planet[] planets ;
    private ArrayAdapter<Planet> listAdapter ;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_main);

        // Find the ListView resource.
        mainListView = (ListView) findViewById( R.id.mainListView );
        ArrayList<Planet> planetList = new ArrayList<Planet>();

        // When item is tapped, toggle checked properties of CheckBox and Planet.
        mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick( AdapterView<?> parent, View item,
                                     int position, long id) {
                Planet planet = listAdapter.getItem( position );
                planet.toggleChecked();
                PlanetViewHolder viewHolder = (PlanetViewHolder) item.getTag();
                viewHolder.getCheckBox().setChecked( planet.isChecked() );
            }
        });

        Button resetButton = (Button) findViewById(R.id.reset);
        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PlanetViewHolder viewHolder = (PlanetViewHolder) view.getTag();

                /*Planet planet = listAdapter.getItem(0);
                planet.toggleChecked();*/

               // listAdapter = new PlanetArrayAdapter(this, planetList);
                mainListView.setAdapter( listAdapter );
                if(viewHolder != null){
                  /*  viewHolder.getCheckBox().setChecked( planet.isChecked() );
                    planet.toggleChecked();*/
                }
            }
        });

        // Create and populate planets.
        planets = (Planet[]) getLastNonConfigurationInstance() ;
        if ( planets == null ) {
            planets = new Planet[] {
                    new Planet("Mercury"), new Planet("Venus"), new Planet("Earth"),
                    new Planet("Mars"), new Planet("Jupiter"), new Planet("Saturn"),
                    new Planet("Uranus"), new Planet("Neptune"), new Planet("Ceres"),
                    new Planet("Pluto"), new Planet("Haumea"), new Planet("Makemake"),
                    new Planet("Eris"), new Planet("Kepler-16b"), new Planet("Kepler-360b"),
                    new Planet("Eureka"), new Planet("intst")
            };
        }

        planetList.addAll( Arrays.asList(planets) );

        // Set our custom array adapter as the ListView's adapter.
        listAdapter = new PlanetArrayAdapter(this, planetList);
        mainListView.setAdapter( listAdapter );
    }

    /** Holds planet data. */
    private static class Planet {
        private String name = "" ;
        private boolean checked = false ;
        public Planet() {}
        public Planet( String name ) {
            this.name = name ;
        }
        public Planet( String name, boolean checked ) {
            this.name = name ;
            this.checked = checked ;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public boolean isChecked() {
            return checked;
        }
        public void setChecked(boolean checked) {
            this.checked = checked;
        }
        public String toString() {
            return name ;
        }
        public void toggleChecked() {
            checked = !checked ;
        }
    }

    public Object onRetainNonConfigurationInstance() {
        return planets ;
    }
}

sample_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/reset"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RESET"
        />

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mainListView">
    </ListView>

</LinearLayout>

sample_simplerow.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@+id/rowTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="16sp" >
    </TextView>

    <CheckBox android:id="@+id/CheckBox01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_alignParentRight="true" android:layout_marginRight="6sp"
        android:focusable="false">
    </CheckBox>

</RelativeLayout>

现在点击重置我想要列表中的所有checbox都应该取消选中

1 个答案:

答案 0 :(得分:4)

在重置按钮上单击,只需迭代planetList并使所有选中为false,下面是示例代码。

for(Planet planet:planetList)
{
planet.setChecked(false);
} 

现在调用listAdapter.notifydatasetchanged()。

如果您有任何疑问,请告诉我。