androidlistviewswitch-statementapplocker

Get Selected Items from ListView


I am developing a AppLock that locks specific apps. For this I have a screen that contains a ListView, and the ListView has multiple Switches, one for every app installed.

I need to know which Switches the user has turned on so and extract the text written in it.

SensitiveApps.java

package com.sr.droidlock;

import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Switch;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SensitiveApps extends ActionBarActivity {

    Switch appName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensitive_apps);
        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
        ArrayList<String> tasks = new ArrayList<String>();
        appName = (Switch) findViewById(R.id.appName) ;

        for (Object object : pkgAppsList)
        {
            ResolveInfo info = (ResolveInfo) object;
            final String title  = (String)((info != null) ? getBaseContext().getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo) : "???");
            tasks.add(title);
        }
        Collections.sort(tasks, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareToIgnoreCase(s2);
            }
        });
        ArrayAdapter<String> adapter = new ArrayAdapter <String>(this,R.layout.layout_sensitive_apps, R.id.appName ,tasks);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }
}

activity_sensitive_apps.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.sr.droidlock.SensitiveApps">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView" />
</LinearLayout>

layout_sensitive_apps

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical" >
    <Switch
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:text="New Switch"
        android:id="@+id/appName" />

</LinearLayout>

Solution

  • Extend ArrayAdapter and override getView method where you need to inflate your own layout. So it'll contain as much switches as you need. Then each switch can implement its own OnCheckedChangeListener so you'll exactly know which switch was changed.

    EDIT

    Create a new class CustomArrayAdapter that extends ArrayAdapter and override getView.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Inflate view
        LayoutInflater inflator = mContext.getLayoutInflater();
        View view = inflator.inflate(R.layout.list_item_topic, null);
    
        switch = (Switch)view.findViewById(R.id.any_switch);
        switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
    
            }
        });
    
        // TODO bind other views or anything else you need
        return view;
    }