在适配器上切换侦听器

时间:2017-10-27 05:09:06

标签: android listview arraylist

我正在将已安装的应用程序显示到列表中,我添加了一个开关Widget,我想在其中设置onstatechange监听器。但到目前为止我还不知道在哪里实施它。

我有getter类

    public class AppList {

       private String name;
       Drawable icon;

       public AppList(String name, Drawable icon) {
           this.name = name;
           this.icon = icon;
       }

       public String getName() {
       return name;
   }

   public Drawable getIcon() {
       return icon;
    }
   }

我尝试在那里添加一个开关但到目前为止没有运气。 这是适配器。

    public class AppAdapter extends BaseAdapter {

   private LayoutInflater layoutInflater;
   private List<AppList> listStorage;

   public AppAdapter(Context context, List<AppList> customizedListView) {
       layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       listStorage = customizedListView;
   }

   @Override
   public int getCount() {
       return listStorage.size();
   }

   @Override
   public Object getItem(int position) {
       return position;
   }

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

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

       ViewHolder listViewHolder;
       if(convertView == null){
           listViewHolder = new ViewHolder();
           convertView = layoutInflater.inflate(R.layout.snippet_list_row, parent, false);

           listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.app_name);
           listViewHolder.imageInListView = (ImageView)convertView.findViewById(R.id.app_icon);

           convertView.setTag(listViewHolder);
       }else{
           listViewHolder = (ViewHolder)convertView.getTag();

       }
       listViewHolder.textInListView.setText(listStorage.get(position).getName());
       listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());

       return convertView;
   }

   static class ViewHolder{

       TextView textInListView;
       ImageView imageInListView;

   }
}

和呼唤

private List<AppList> getInstalledApps() {
   List<AppList> res = new ArrayList<AppList>();
   List<PackageInfo> packs = getActivity().getPackageManager().getInstalledPackages(0);
   for (int i = 0; i < packs.size(); i++) {
       PackageInfo p = packs.get(i);
       if ((isSystemPackage(p) == true)) {
           String appName = p.applicationInfo.loadLabel(getActivity().getPackageManager()).toString();
           Drawable icon = p.applicationInfo.loadIcon(getActivity().getPackageManager());
           res.add(new AppList(appName, icon));
       }
   }
   return res;
}

如何为每一行实现一个侦听器。

1 个答案:

答案 0 :(得分:0)

所以我能够在getview()上创建它:在这里我做了什么。

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder listViewHolder;
    if(convertView == null){
        listViewHolder = new ViewHolder();
        convertView = layoutInflater.inflate(R.layout.snippet_list_row, parent, false);

        listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.app_name);
        listViewHolder.imageInListView = (ImageView)convertView.findViewById(R.id.app_icon);
        listViewHolder.aSwitch = convertView.findViewById(R.id.Switch_Block);

        final View finalConvertView = convertView;
        listViewHolder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // do something, the isChecked will be
                // true if the switch is in the On position
                Log.w("OnChange" ,listStorage.get(position).getName() )

            }
        });

        convertView.setTag(listViewHolder);
    }else{
        listViewHolder = (ViewHolder)convertView.getTag();

    }
    listViewHolder.textInListView.setText(listStorage.get(position).getName());
    listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());

    return convertView;
}