Solved

Android Custome ListView with checkbox OnCheckedChangeListener

Posted on 2011-09-21
3
1,569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

695 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