Link to home
Start Free TrialLog in
Avatar of russellC
russellCFlag for United States of America

asked on

Android Custome ListView with checkbox OnCheckedChangeListener

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

Avatar of alexey_gusev
alexey_gusev
Flag of United Kingdom of Great Britain and Northern Ireland image

I bet that

lv1.findViewById(R.id.listneeded)

Open in new window


returns null. You should do your setter in getView() of the adapter
Avatar of russellC

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of alexey_gusev
alexey_gusev
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial