Solved

Android Custome ListView with checkbox OnCheckedChangeListener

Posted on 2011-09-21
3
1,555 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 23
How to parse custom JSON to POJO java 4 56
Java DateChooser? 3 29
jar file executable 12 43
Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

895 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

21 Experts available now in Live!

Get 1:1 Help Now