ListView + CheckBox implements full and inverse selection, listviewcheckbox

Source: Internet
Author: User

ListView + CheckBox implements full and inverse selection, listviewcheckbox

The implementation is as follows: (in the example of the first line of code modified). When you click Select All, the effect is as follows:



Select only the following:



Press and select inverse,



First, layout:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "# f2f0eb" android: orientation = "vertical"> <FrameLayout android: layout_width = "match_parent" android: layout_height = "match_parent" android: layout_weight = "50"> <ListView android: id = "@ + id/list_view" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "@ android: color/transparent" android: cacheColorHint = "#00000000" android: divider = "# D4D4D4" android: dividerHeight = "1px" android: fastScrollEnabled = "true" android: overScrollMode = "never"/> </FrameLayout> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_marginBottom = "10dip" android: layout_marginLeft = "5dp" android: layout_marginRight = "5dip" android: layout_marginTop = "10dp" android: layout_weight = "1" android: background = "@ drawable/button_register_bg" android: gravity = "center" android: onClick = "selectAll" android: paddingBottom = "7dp" android: paddingTop = "7dp" android: text = "select all" android: textColor = "@ android: color/white" android: textSize = "18sp"/> <Button android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_marginBottom = "10dip" android: layout_marginLeft = "5dp" android: layout_marginRight = "5dip" android: layout_marginTop = "10dp" android: layout_weight = "1" android: background = "@ drawable/button_login_bg" android: gravity = "center" android: onClick = "selectOppo" android: paddingBottom = "7dp" android: paddingTop = "7dp" android: text = "invert" android: textColor = "@ android: color/white" android: textSize = "18sp"/> </LinearLayout>

Note that ListView is nested in FrameLayout and the layout_weight attribute is specified (otherwise, the following two buttons are hidden .)

In MainActivity:

public void selectAll(View view){for(Fruit fruit : fruitList){fruit.setChecked(true);}adapter.notifyDataSetChanged();}public void selectOppo(View view){for(Fruit fruit : fruitList){fruit.setChecked(!fruit.isChecked());}adapter.notifyDataSetChanged();}

This is done.

Complete code:

MainActivity:

package com.example.uilistviewtest;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.Toast;import com.example.uilistviewtest.FruitAdapter.ViewHolder;public class MainActivity extends Activity {private List<Fruit> fruitList = new ArrayList<Fruit>();private FruitAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initFruits();adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item, fruitList);ListView listView = (ListView) findViewById(R.id.list_view);listView.setAdapter(adapter);listView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Fruit fruit = fruitList.get(position);ViewHolder holder = (ViewHolder) view.getTag();if (fruit.isChecked()) {fruit.setChecked(false);holder.cb.setChecked(false);} else {fruit.setChecked(true);holder.cb.setChecked(true);}Toast.makeText(MainActivity.this, fruit.getName(),Toast.LENGTH_SHORT).show();}});}public void selectAll(View view){for(Fruit fruit : fruitList){fruit.setChecked(true);}adapter.notifyDataSetChanged();}public void selectOppo(View view){for(Fruit fruit : fruitList){fruit.setChecked(!fruit.isChecked());}adapter.notifyDataSetChanged();}private void initFruits() {Fruit apple = new Fruit("Apple", R.drawable.apple_pic);fruitList.add(apple);Fruit banana = new Fruit("Banana", R.drawable.banana_pic);fruitList.add(banana);Fruit orange = new Fruit("Orange", R.drawable.orange_pic);fruitList.add(orange);Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);fruitList.add(watermelon);Fruit pear = new Fruit("Pear", R.drawable.pear_pic);fruitList.add(pear);Fruit grape = new Fruit("Grape", R.drawable.grape_pic);fruitList.add(grape);Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);fruitList.add(pineapple);Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);fruitList.add(strawberry);Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic);fruitList.add(cherry);for (int i = 0; i < 10; i++) {Fruit mango = new Fruit("Mango", R.drawable.mango_pic);fruitList.add(mango);}}}

Fruit business bean class:

package com.example.uilistviewtest;public class Fruit {private String name;private int imageId;private boolean checked;public Fruit(String name, int imageId) {this.name = name;this.imageId = imageId;}public String getName() {return name;}public int getImageId() {return imageId;}public boolean isChecked() {return checked;}public void setChecked(boolean checked) {this.checked = checked;}}

Adapter:

package com.example.uilistviewtest;import java.util.List;import android.content.Context;import android.support.v4.widget.CursorAdapter;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.CheckBox;import android.widget.ImageView;import android.widget.SimpleAdapter;import android.widget.TextView;public class FruitAdapter extends ArrayAdapter<Fruit> {private int resourceId;public FruitAdapter(Context context, int textViewResourceId,List<Fruit> objects) {super(context, textViewResourceId, objects);resourceId = textViewResourceId;}CursorAdapter ca;@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Fruit fruit = getItem(position);View view;ViewHolder viewHolder;if (convertView == null) {view = LayoutInflater.from(getContext()).inflate(resourceId, null);viewHolder = new ViewHolder();viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);viewHolder.cb = (CheckBox) view.findViewById(R.id.cb);view.setTag(viewHolder);} else {view = convertView;viewHolder = (ViewHolder) view.getTag();}viewHolder.fruitImage.setImageResource(fruit.getImageId());viewHolder.fruitName.setText(fruit.getName());viewHolder.cb.setChecked(fruit.isChecked());return view;}class ViewHolder {ImageView fruitImage;TextView fruitName;CheckBox cb;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.