In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to achieve listview with pictures and checkbox in Android" related knowledge, editor through the actual case to show you the process of operation, the method of operation is simple and fast, practical, I hope that this "how to achieve listview with pictures and checkbox in Android" article can help you solve the problem.
First create a new MyAdapter class, which inherits from BaseAdapter. To achieve the binding of data in MyAdapter, I write the data to death because it is tested here.
Java code
Package com.zwq.umeng; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView Public class MyAdapter extends BaseAdapter {private LayoutInflater mInflater; private List mData; public static Map isSelected; public MyAdapter (Context context) {mInflater = LayoutInflater.from (context); init ();} / initialize private void init () {mData=new ArrayList (); for (int I = 0; I < 5) Map map +) {Map map = new HashMap (); map.put ("img", R.drawable.icon); map.put ("title", "th" + (I + 1) + "Line title"); mData.add (map) } / / the map of isSelected is defined here to record the state of each listitem, and the initial state is all false. IsSelected = new HashMap (); for (int I = 0; I < mData.size (); iTunes +) {isSelected.put (I, false);}} @ Override public int getCount () {return mData.size ();} @ Override public Object getItem (int position) {return null } @ Override public long getItemId (int position) {return 0;} @ Override public View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder = null; / / convertView initializes convertView when it is null. If (convertView = = null) {holder = new ViewHolder (); convertView = mInflater.inflate (R.layout.vlist, null); holder.img = (ImageView) convertView.findViewById (R.id.img); holder.title = (TextView) convertView.findViewById (R.id.title); holder.cBox = (CheckBox) convertView.findViewById (R.id.cb) ConvertView.setTag (holder);} else {holder = (ViewHolder) convertView.getTag ();} holder.img.setBackgroundResource ((Integer) mData.get (position). Get ("img")); holder.title.setText (mData.get (position) .get ("title"). ToString ()) Holder.cBox.setChecked (isSelected.get (position)); return convertView;} public final class ViewHolder {public ImageView img; public TextView title; public CheckBox cBox;}}
Note the handling of the data in the getView () method in the above class.
The next step is to create a new list.xml file, which is the layout image, textview, checkbox.
Java code
The sentence android:layout_weight= "1" can increase the weight of the middle textview, so that the following checkbok can be displayed on the right.
Android:focusable= "false" android:focusableInTouchMode= "false" android:clickable= "false"
These three sentences are very important. If you don't add them, there will be mistakes.
Because the click event priority of checkbox is higher than that of listview, android:focusable= "false" is added to checkbox so that checkbox does not get focus initially.
Next, add the Listview component to main.xml
Java code
The next step is to call in activity:
Java code
List= (ListView) findViewById (R.id.lv); MyAdapter adapter=new MyAdapter (this); list.setAdapter (adapter); list.setItemsCanFocus (false); list.setChoiceMode (ListView.CHOICE_MODE_MULTIPLE) List.setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView parent, View view, int position, long id) {ViewHolder vHollder = (ViewHolder) view.getTag (); / / changes the state of checkbox for each click and modifies the value of map. VHollder.cBox.toggle (); MyAdapter.isSelected.put (position, vHollder.cBox.isChecked ();}})
Take a look at the effect picture:
To get which items have been selected, you can test them like this:
Java code
OnClickListener bPop = new OnClickListener () {@ Override public void onClick (View v) {for (int iTuno)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.