80809

listview with checkbox status changed when scoll [duplicate]

Question:

<blockquote>

<strong>Possible Duplicate:</strong><br /><a href="https://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list" rel="nofollow">Getting an issue while checking the dynamically generated checkbox through list view</a>

</blockquote>

I have listview with checkboxes.Bydefault all checkboxes are checked.After that some checkboxes unchecked and sroll those checkboxes are checked automatically.But i want after scroll the listview those checkboxes are checked. My code is:

public class FriendListAdapter extends BaseAdapter { private LayoutInflater mInflater; DrunkMessages friendsList; boolean[] checkBoxState; public FriendListAdapter(DrunkMessages friendsList) { this.friendsList = friendsList; if (Utility.model == null) { Utility.model = new FriendsGetProfilePics(); } Utility.model.setListener(this); mInflater = LayoutInflater.from(friendsList.getBaseContext()); checkBoxState=new boolean[jsonArray.length()]; } public int getCount() { return jsonArray.length(); } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(final int position, View convertView, ViewGroup parent) { View hView = getLayoutInflater().inflate(R.layout.friendslist, null); ImageView profile_pic = (ImageView) hView.findViewById(R.id.profile_pic); TextView name = (TextView) hView.findViewById(R.id.name); final CheckBox checkbox=(CheckBox)hView.findViewById(R.id.checkbox); profile_pic.setImageBitmap(Utility.model.getImage(friendid[position], pictures[position])); name.setText(names[position]); checkbox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(((CheckBox)v).isChecked()){ checkBoxState[position]=true; status[position]="1"; Log.e("checked","checked"); Log.e("Checked",status[position]); } else{ checkBoxState[position]=false; status[position]="0"; Log.e("checked","unchecked"); Log.e("Checked",status[position]); } } }); return hView; }

Answer1:

convertview may give you previously generated view so dont rely on it. just store your checkbox state's in one boolean array & change that accordingly depends on checkchangelistener callback here is code for you I just modified getView method of your code everything else keep as is

public View getView(final int position, View convertView, ViewGroup parent) { View hView = getLayoutInflater().inflate(R.layout.friendslist, null); ImageView profile_pic = (ImageView) hView.findViewById(R.id.profile_pic); TextView name = (TextView) hView.findViewById(R.id.name); final CheckBox checkbox=(CheckBox)hView.findViewById(R.id.checkbox); checkbox.setTag(new Integer(position)); profile_pic.setImageBitmap(Utility.model.getImage(friendid[position], pictures[position])); name.setText(names[position]); checkbox.setOnCheckedChangeListener(null); if(checkBoxState[position]) checkbox.setChecked(true); else checkbox.setChecked(false); checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Integer pos = (Integer)buttonView.getTag(); if(isChecked){ checkBoxState[pos.intValue()]=true; } else{ checkBoxState[pos.intValue()]=false; Log.e("checked","unchecked"); } } }); return hView; }

Answer2:

instead of inflating another xml for checkbox you can take a listview with simple_list_item_multiple_choice attribute. like this

adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_multiple_choice,obje‌​ctOfList);

Answer3:

Add the following code in your getview method

if(checkBoxState.length>0) checkbox.setChecked(checkBoxState[position]);

Recommend

  • Select image from gridview and display it on other intent
  • Can not cast IMAPInputStream to Multipart
  • How to store items of multiple JSON Array and display it listview?
  • Navigation drawer is always inflated
  • Angular2 How to display localStorage value inside HTML5 template?
  • How to keep button state across different pages?
  • Checking a play current mode makes an error occur
  • Fat binaries in iOS
  • Building jamvm 1.5.4 on OS X Lion
  • runtime error when linking ffmpeg libraries in qt creator
  • Getting media player state in windows phone 7
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • GridView breaks while scrolling
  • Blackberry - Custom EditField Cursor
  • How do I get HTML corresponding to current DOM tree?
  • Content-Length header not returned from Pylons response
  • Play WS (2.2.1): post/put large request
  • Body moving without any force applied? (Box2d)
  • How to access EntityManager inside Entity class in EJB3
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • vba code to select only visible cells in specific column except heading
  • How to show dropdown in excel using jrxml (jasper api)?
  • AT Commands to Send SMS not working in Windows 8.1
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Android Google Maps API OnLocationChanged only called once
  • LevelDB C iterator
  • Is it possible to post an object from jquery to bottle.py?
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?