Solved

Android Custome ListView with checkbox OnCheckedChangeListener

Posted on 2011-09-21
3
1,557 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
ID: 36581309
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
ID: 36593782
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
ID: 36597148
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 25
Java exception bubble up 2 18
Error in @AspectJ Based AOP with Spring 2 13
jsp insert to database example 2 24
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

810 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