Android Open Source - app_list_widget App List Select Adapter






From Project

Back to project page app_list_widget.

License

The source code is released under:

Apache License

If you think the Android project app_list_widget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2013 acbelter//ww w.  ja  va  2s.  co  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.acbelter.applistwidget.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.acbelter.applistwidget.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class AppListSelectAdapter extends ArrayAdapter<AppItem> implements Filterable {
    private LayoutInflater mInflater;
    private List<AppItem> mAppsList;
    private Set<AppItem> mCheckedApps;
    private Filter mAppsFilter;

    public AppListSelectAdapter(Context context,
                                ArrayList<AppItem> appsList,
                                ArrayList<AppItem> checkedApps) {
        super(context, R.layout.item_app_select, appsList);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mAppsList = appsList;
        mCheckedApps = new HashSet<AppItem>();

        for (AppItem item : checkedApps) {
            for (int i = 0; i < appsList.size(); i++) {
                if (item.getPackageName().equals(appsList.get(i).getPackageName())) {
                    mCheckedApps.add(appsList.get(i));
                }
            }
        }
    }

    public ArrayList<AppItem> getCheckedApps() {
        return new ArrayList<AppItem>(mCheckedApps);
    }

    static class ViewHolder {
        ImageView icon;
        TextView appName;
        TextView packageName;
        CheckBox checkBox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_app_select, null);

            final ViewHolder holder = new ViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.app_icon);
            holder.appName = (TextView) convertView.findViewById(R.id.app_name);
            holder.packageName = (TextView) convertView.findViewById(R.id.app_package);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.select_check_box);
            convertView.setTag(holder);
        }

        final ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        final AppItem appItem = mAppsList.get(position);
        viewHolder.icon.setImageDrawable(appItem.getIcon());
        viewHolder.appName.setText(appItem.getAppName());
        viewHolder.packageName.setText(appItem.getPackageName());
        viewHolder.checkBox.setTag(appItem);

        if (mCheckedApps.contains(appItem)) {
            viewHolder.checkBox.setChecked(true);
        } else {
            viewHolder.checkBox.setChecked(false);
        }

        viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mCheckedApps.add((AppItem) viewHolder.checkBox.getTag());
                } else {
                    mCheckedApps.remove(viewHolder.checkBox.getTag());
                }
            }
        });

        return convertView;
    }

    @Override
    public Filter getFilter() {
        if (mAppsFilter == null) {
            mAppsFilter = new AppListFilter();
        }
        return mAppsFilter;
    }

    class AppListFilter extends Filter {
        private List<AppItem> mFullAppList;

        public AppListFilter() {
            mFullAppList = mAppsList;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                results.values = mFullAppList;
                results.count = mFullAppList.size();
            } else {
                mAppsList = mFullAppList;
                List<AppItem> filteredApps = new ArrayList<AppItem>();

                for (int i = 0; i < mAppsList.size(); i++) {
                    if (mAppsList.get(i).getAppName().toUpperCase()
                            .contains(constraint.toString().toUpperCase())) {
                        filteredApps.add(mAppsList.get(i));
                    }
                }

                results.values = filteredApps;
                results.count = filteredApps.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mAppsList = (List<AppItem>) results.values;
            notifyDataSetChanged();
        }
    }
}




Java Source Code List

com.acbelter.applistwidget.ui.AppItem.java
com.acbelter.applistwidget.ui.AppListOrderAdapter.java
com.acbelter.applistwidget.ui.AppListSelectAdapter.java
com.acbelter.applistwidget.ui.AppOrderFragment.java
com.acbelter.applistwidget.ui.AppSelectFragment.java
com.acbelter.applistwidget.ui.ConfigActivity.java
com.acbelter.applistwidget.ui.SeekBarPreference.java
com.acbelter.applistwidget.ui.SettingsActivity.java
com.acbelter.applistwidget.ui.SettingsFragment.java
com.acbelter.applistwidget.widget.AppListLargeWidgetProvider.java
com.acbelter.applistwidget.widget.AppListWidgetFactory.java
com.acbelter.applistwidget.widget.AppListWidgetProvider.java
com.acbelter.applistwidget.widget.AppListWidgetService.java
com.acbelter.applistwidget.widget.Pref.java