Solved

Android Custome ListView with checkbox OnCheckedChangeListener

Posted on 2011-09-21
3
1,550 Views
Last Modified: 2012-05-12
The setting op of my click listener for each checkbox in my custom list items is causing the application to crash.

Below is a copy of the code section that I have made to got through and load up each listViewItem.
//SET UP MAIN LIST
        lv1 = (ListView)findViewById(R.id.mainList);
        double listTotal = 0;
        
       if(!itemList.isEmpty())
        {
    	   //Toast.makeText(this, "Entered Setup Main loop", Toast.LENGTH_SHORT).show();
    	   
        	ArrayList<HashMap<String, String>> mylist = 
		        	new ArrayList<HashMap<String,String>>();
        	 
        	for (int i = 0; i <= itemList.size()-1; i++)
        	{       			        
		        HashMap<String, String> map = new HashMap<String, String>();
		        map.put("id", Integer.toString(itemList.get(i).getItem_id()));
		        map.put("top", itemList.get(i).getName());
		        map.put("bottom", String.valueOf(itemList.get(i).getPrice()));
		        mylist.add(map);
		        listTotal = listTotal + itemList.get(i).getPrice();
		        
		        final String name = itemList.get(i).getName();
		        
		        CheckBox needItem = (CheckBox)lv1.findViewById(R.id.listneeded);      
		        needItem.setOnCheckedChangeListener(new OnCheckedChangeListener()
		        {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						//createToast("Item: " + name);
					}
		        });
        	}
        	
	        SimpleAdapter lItems = new SimpleAdapter(this,mylist,R.layout.listitemlayout,
	        		new String[]{"id", "top", "bottom"}, new int[]{R.id.itemid, R.id.topText, R.id.bottomText});
	        
	        lv1.setAdapter(lItems);
	        
        }

Open in new window

0
Comment
Question by:russellC
  • 2
3 Comments
 
LVL 24

Expert Comment

by:alexey_gusev
Comment Utility
I bet that

lv1.findViewById(R.id.listneeded)

Open in new window


returns null. You should do your setter in getView() of the adapter
0
 
LVL 3

Author Comment

by:russellC
Comment Utility
Sorry for the delayed responce.  

Your right my lv1.findViewById(R.id.listneeded) is returning null. I think I may be going about settign up this listView wrong.  

Is there a better example that shows how to set the eventListeners for the custome listView items?
0
 
LVL 24

Accepted Solution

by:
alexey_gusev earned 500 total points
Comment Utility
no, your listview is OK, but its items (ie individual lines) are defined in getView() method of the adapter. Android asks your adapter to build a view for given index - and that's where you create your custom lines, so you can setup it there.

have a look at this video - http://www.google.com/intl/it-IT/events/io/2010/sessions/world-of-listview-android.html - that's the guy behind many Android features :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now