Search code examples
androidlistviewcheckboxreloadchecked

Android - How to ckeck an item in ListView without selecting it?


I am displaying running tasks in a ListView(simple_list_item_multiple_choice) and removing checked tasks from the list using button's onClick event as we can not kill the tasks completely in android so i have set the Timer to reload these tasks after every 5 seconds.

I am using one simple CheckBox widget to check all these CheckBox of Listview at once.

So my problem is that i want new items to be checked at the time of reload if my CheckBox widget is set to checked but i am unable to do that.

Here is my code :-

public void reloadTasks()
{
    int listInitSize = list.size();
    try
    {
        List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
        int numOfTasks = tasks.size();
        for(int i = 0; i < numOfTasks; i++)
        {
            ActivityManager.RunningAppProcessInfo task = tasks.get(i);
            boolean doAdd = true;
            HashMap<String, String> item = new HashMap<String, String>();
            item.put("Process", task.processName);
            try
            {
                PackageInfo myPInfo = getPackageManager().getPackageInfo(task.processName, 0);
                item.put("Name", myPInfo.applicationInfo.loadLabel(getPackageManager()).toString());
            }
            catch (PackageManager.NameNotFoundException ne)
            {

            }
            if(!list.isEmpty())
            {
                if(list.contains(item))
                {
                    doAdd = false;
                }
                else
                {
                    doAdd = true;
                }
            }
            if(filter == true)
            {
                int size = SystemProcessList.length;
                for (int j = 0; j < size; j++)
                {
                    if(task.processName.indexOf(SystemProcessList[j]) > -1)
                    {
                        doAdd = false;
                    }
                }
            }
            if(doAdd==true)
            {
                addItem(item);
            }

        }
    }
    catch (SecurityException se)
    {

    }
    notes.notifyDataSetChanged();
    int size = list.size();
    int numOfLoop = list.size() - listInitSize;
    for(int i = 1; i >= numOfLoop; i++)
    {
        if(cb.isChecked())
        {
            lv.setItemChecked(size-i, true);
        }
    }

}

I tried something to check the items at reload but it's not working and this attempt is not letting my app to get load on emulator(not force close just a blank screen).


Solution

  • You should use a listadapter and create a class that extends ArrayAdapter and then in your getView function you should implement each row. It is not so much easy, you can see a working example here

    http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/