dimanche 12 juillet 2015

insert checked item from a listview into Database

I am developing an application that shows the user a listView of the applications founded in his device. Each row of the ListView contain a checkBox and the icon of the app. this listView appear in alert dialog .I want to save the checked item into the database when the user click the positive button in the alert dialog. Any help?

code of the Adapter of the listView:

    public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {

    private List<ApplicationInfo> appsList = null;
    private Context context;
    private PackageManager packageManager;

    public ApplicationAdapter(Context context, int textViewResourceId,
                              List<ApplicationInfo> appsList) {
        super(context, textViewResourceId, appsList);
        this.context = context;
        this.appsList = appsList;
        packageManager = context.getPackageManager();

    }

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

    @Override
    public ApplicationInfo getItem(int position) {
        return ((null != appsList) ? appsList.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.snippet_list_row, null);

        }

        ApplicationInfo applicationInfo = appsList.get(position);
        if (null != applicationInfo) {
            TextView appName = (TextView) view.findViewById(R.id.app_name);
            TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
            ImageView iconView = (ImageView) view.findViewById(R.id.app_icon);
            CheckBox appChecked = (CheckBox) view.findViewById(R.id.app_checked);

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

        }
        return view;
    }
};

Code of the alert dialog:

        @Override
      public boolean onOptionsItemSelected(MenuItem item) {

    boolean result = true;

    switch (item.getItemId()) {
        case R.id.action_settings: {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.setTitle("Select allowed Apps");
            View view=getLayoutInflater().inflate(R.layout.single_view,null);
            builder.setView(view);

            lView = (ListView) view.findViewById(R.id.list1);
            new LoadApplications().execute();

            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {


                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();

                }
            });
            AlertDialog alert = builder.create();
            alert.show();
            break;
        }
        default: {
            result = super.onOptionsItemSelected(item);

            break;
        }
    }

    return result;

}




Aucun commentaire:

Enregistrer un commentaire