0

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>
Sanket B
  • 1,070
  • 1
  • 9
  • 23
  • I think you will need to store the states of switches in a POJO class. So you will have `String taskName` and `boolean taskState` as fields of the class and save the state of switch to the corresponding item. – K Neeraj Lal Mar 14 '16 at 12:57
  • You can maintain a boolean[] array to keep track of all the switches for each position in the list. Example: initially all are false say! When a switch is turned on lets say position 2 in list then array[2] = true; that way can keep track easily. – Sjd Mar 14 '16 at 13:06

2 Answers2

2

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;
}
Honza Musil
  • 320
  • 2
  • 10
  • Do you mean that I should extend my class SensitiveApps with ArrayAdapter and then override the getView method? And what should be done in getView? – Sanket B Mar 14 '16 at 16:17
1

I think you can achieve your goal if you create getter and setter for your switch in your model. And for your installed app(on user click) you set some value for example setInstalledApp(true); And when you loop trough your array check with switch getter which app user is clicked. For example:

if(switch.isInstalledApp()){
    //get item data
}else{

} 

Hope to help!

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37