Reputation: 1751
I have an application in which i have reset button on the top of the ListView and on clicking the Reset view all items in the Listview which are checked should get unchecked.
I have tried the code from following link
http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php
The snippets are :
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>
Now on clicking the reset I want that all the checboxes in the list should get unchecked
Upvotes: 3
Views: 3473
Reputation: 1371
On reset button click , Just iterate the planetList and make all checked false, below is sample code.
for(Planet planet:planetList)
{
planet.setChecked(false);
}
now call listAdapter.notifydatasetchanged().
If you have any query please let me know.
Upvotes: 4