jeudi 23 juin 2016

Adding Checkbox In listView To select multiple apps for further action

Please help me with putting checkbox in listview to select multiple apps from listview and do further actions on them using buttons.

Adapter class.

public class AppsAdapter extends ArrayAdapter<ApplicationInfo>{
private List<ApplicationInfo> applist=null;
private Context context;
private PackageManager packageManager;

public AppsAdapter(Context context, int resource,
                   List<ApplicationInfo> objects){
    super(context,resource,objects);
    this.context=context;
    this.applist=objects;
    packageManager=context.getPackageManager();
}

@Override
public int getCount(){
    return ((null != applist)?applist.size():0);
}

@Override
public ApplicationInfo getItem(int position){
    return ((null != applist)?applist.get(position):null);
}

@Override
public long getItemId(int position){
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view=convertView;

    if(null==view){
        LayoutInflater layoutInflater=      (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view=layoutInflater.inflate(R.layout.item_list,null);
    }
    ApplicationInfo data=applist.get(position);

    if(null!=data){
        TextView appName=(TextView)view.findViewById(R.id.app_name);
        TextView packageName=(TextView)view.findViewById(R.id.package_name);
        ImageView iconView=(ImageView)view.findViewById(R.id.image_icon);
       // CheckedTextView checkedTextView=    (CheckedTextView)view.findViewById(R.id.checkedTextView1);

        appName.setText(data.loadLabel(packageManager));
        packageName.setText(data.packageName);
        iconView.setImageDrawable(data.loadIcon(packageManager));


    }
    return view;
 }
 }

MainActivity class.

Where should i exactly define listview and checkedtextview in mainactivity.

public class MainActivity extends ListActivity {

private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private AppsAdapter listadapter = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    packageManager = getPackageManager();

    new LoadApplications().execute();
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    ApplicationInfo app = applist.get(position);


    try{
        Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);

        if(intent != null) {
            startActivity(intent);
        }
    } catch(ActivityNotFoundException e) {
        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
    } catch(Exception e) {
        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
 }

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {

    ArrayList<ApplicationInfo> appList = new ArrayList<ApplicationInfo>();

    for(ApplicationInfo info : list) {
        try{
            if(packageManager.getLaunchIntentForPackage(info.packageName) != null) {
                appList.add(info);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    return appList;
}

private class LoadApplications extends AsyncTask<Void, Void, Void> {

    private ProgressDialog progress = null;


    @Override
    protected Void doInBackground(Void... params) {


        applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));

        listadapter = new AppsAdapter(MainActivity.this, R.layout.item_list, applist);


        return null;
    }

}
}

item_list.xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/image_icon"
    android:scaleType="centerCrop"
    android:padding="3dp"
    />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:gravity="center|center_vertical"
    android:paddingLeft="5dp"
    >

    <TextView
    android:id="@+id/app_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:textStyle="bold" />

    <TextView
        android:id="@+id/package_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical" />


</LinearLayout>
</LinearLayout>




Aucun commentaire:

Enregistrer un commentaire