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

Posted

技术标签:

【中文标题】如何在单击 ListView 中的按钮时取消选中所有列表项【英文标题】:How to uncheck all the List item on clicking a button in ListView 【发布时间】:2015-03-10 00:01:01 【问题描述】:

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

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

sn-ps 是:

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_
    android:layout_>

    <Button
        android:id="@+id/reset"
        android:layout_
        android:layout_
        android:text="RESET"
        />

    <ListView android:layout_
        android:layout_
        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_
    android:layout_>

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

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

</RelativeLayout>

现在单击重置时,我希望列表中的所有复选框都应取消选中

【问题讨论】:

【参考方案1】:

点击重置按钮时,只需迭代planetList并将所有检查设为false,下面是示例代码。

for(Planet planet:planetList)

planet.setChecked(false);
 

现在调用 listAdapter.notifydatasetchanged()。

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

【讨论】:

以上是关于如何在单击 ListView 中的按钮时取消选中所有列表项的主要内容,如果未能解决你的问题,请参考以下文章

取消选择 ListView 中的项目

使用jquery单击时,单击单选按钮取消选中

如何防止芯片被取消选中?

单击模态对话框按钮后如何取消选中该复选框?

vb.net 点击按钮选中listview最后一行

为啥在单击“全选”按钮后,复选框会被选中,然后又被取消选中?