2017-07-24 28 views
0

我有一个应用程序,其中包含两个arraylist即“a”和“b”,当我想禁用从列表中的项目切换按钮“a”它会自动禁用所有其他项目在列表视图中。请帮助我。适配器的如何启用/禁用根据Android的列表视图项目索引切换?

代码: -

private LayoutInflater layoutInflater; 
    private List<AllAppList> listStorage; 
    private Context mContext; 
    ArrayList<WhiteListModel> newDataSet, existingDataSet; 
    private String TAG = AppAdapter.class.getSimpleName(); 
    private MySharedPreference sharedPreference; 
    private WhiteListModel whiteListModel; 
    private Gson gson; 

    public AppAdapter(Context context, List<AllAppList> customizedListView) { 
     layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     listStorage = customizedListView; 
     this.mContext = context; 
     existingDataSet = new ArrayList<>(); 
     newDataSet = new ArrayList<>(); 
     gson = new Gson(); 
     sharedPreference = new MySharedPreference(mContext); 
     whiteListModel = new WhiteListModel(); 

     //retrieve data from shared preference 
     String jsonScore = sharedPreference.getAppsArrayListData(); 
     Type type = new TypeToken<ArrayList<WhiteListModel>>() { 
     }.getType(); 
     existingDataSet = gson.fromJson(jsonScore, type); 
    } 

    @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(final int position, View convertView, ViewGroup parent) { 
     final ViewHolder listViewHolder; 
     if (convertView == null) { 
      listViewHolder = new ViewHolder(); 
      convertView = layoutInflater.inflate(R.layout.installed_app_list_item, parent, false); 

      listViewHolder.textInListView = (TextView) convertView.findViewById(R.id.list_app_name); 
      listViewHolder.imageInListView = (ImageView) convertView.findViewById(R.id.app_icon); 
      listViewHolder.switchCompat = (SwitchCompat) convertView.findViewById(R.id.toggleButton); 
      convertView.setTag(listViewHolder); 
     } else { 
      listViewHolder = (ViewHolder) convertView.getTag(); 
     } 
     listViewHolder.textInListView.setText(listStorage.get(position).getName()); 
     listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon()); 

     boolean isChecked = false; 
     AllAppList model = listStorage.get(position); 
     if (existingDataSet!=null){ 
      for (int i = 0; i < existingDataSet.size(); i++) { 
       if (model.getPackName().equalsIgnoreCase(existingDataSet.get(i).getPackName())) { 
        isChecked = true; 

       } 
      } 
     } 
     listViewHolder.switchCompat.setChecked(true); 

     listViewHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(final CompoundButton buttonView, boolean isChecked) { 
       if (isChecked){ 
        new AlertDialog.Builder(mContext, R.style.AppCompatAlertDialogStyle).setTitle("Warning").setMessage("You want to whiteList this application?").setPositiveButton("YES", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          //Adding items in Dataset 
          AllAppList appList = listStorage.get(position); 
          whiteListModel.setName(appList.getName()); 
          whiteListModel.setPackName(appList.getPackName()); 


          if (existingDataSet != null) { 
           existingDataSet.add(whiteListModel); 
           saveScoreListToSharedpreference(existingDataSet); 
          } else { 
           newDataSet.add(whiteListModel); 
           saveScoreListToSharedpreference(newDataSet); 
          } 
          //Notifying adapter data has been changed..... 
          notifyDataSetChanged(); 
          listViewHolder.switchCompat.setChecked(false); 

         } 
        }).setNegativeButton("No", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          listViewHolder.switchCompat.setChecked(false); 
         } 
        }).show(); 
       }else { 
        listViewHolder.switchCompat.setChecked(true); 
       } 



      } 
     }); 
     return convertView; 
    } 

    /** 
    * Save list of scores to own sharedpref 
    * 
    * @param whiteListApps 
    */ 
    private void saveScoreListToSharedpreference(ArrayList<WhiteListModel> whiteListApps) { 
     Gson gson = new Gson(); 
     //convert ArrayList object to String by Gson 
     String jsonScore = gson.toJson(whiteListApps); 
     Log.e(TAG, "LIST::" + jsonScore); 
     //save to shared preference 
     sharedPreference.saveAppsArrayListData(jsonScore); 
    } 

    private static class ViewHolder { 
     static SwitchCompat switchCompat; 
     TextView textInListView; 
     ImageView imageInListView; 
    } 

} 
+0

可以使用SparseBooleanArray用于存储和retriveing的位置状态是否是真还是假。并相应地设置switchCompat。 –

+0

你能编辑我的代码plsss – Raghav

+0

我是新的............ – Raghav

回答

0

如果您在使用一个ViewHolder 静态组件,这意味着你要创建只有一个对象的实例。 因此,在这种情况下,所有按钮都只有一个按钮。如果更改每一行的每个属性,它将在所有行中更改。

使用此viewHolder,而不是你的 “viewHolder”

private class ViewHolder { 
     SwitchCompat switchCompat; 
     TextView textInListView; 
     ImageView imageInListView; 
    } 
+0

已经使用......... – Raghav

+0

你使用了一个静态的SwitchCompat。您没有在ViewHolder中使用静态组件。你有没有静态表达尝试它? – saeid

0

声明中WhiteListModel类布尔变量。并写入getter setter。

在您的代码中添加这些行。

private SparseBooleanArray sparseBooleanArray = new SparseBooleanArray(); 

在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.installed_app_list_item, parent, false); 

      listViewHolder.textInListView = (TextView) convertView.findViewById(R.id.list_app_name); 
      listViewHolder.imageInListView = (ImageView) convertView.findViewById(R.id.app_icon); 
      listViewHolder.switchCompat = (SwitchCompat) convertView.findViewById(R.id.toggleButton); 
      convertView.setTag(listViewHolder); 
     } else { 
      listViewHolder = (ViewHolder) convertView.getTag(); 
     } 

     **sparseBooleanArray.put(position,whiteListModel.getIsSwitchOn());** 
     listViewHolder.textInListView.setText(listStorage.get(position).getName()); 
     listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon()); 

     boolean isChecked = false; 
     AllAppList model = listStorage.get(position); 
     if (existingDataSet!=null){ 
      for (int i = 0; i < existingDataSet.size(); i++) { 
       if (model.getPackName().equalsIgnoreCase(existingDataSet.get(i).getPackName())) { 
        isChecked = true; 
       } 
      } 
     } 
     **listViewHolder.switchCompat.setChecked(whiteListModel.getIsSwitchOn());** 

     listViewHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) { 
       if (isChecked){ 
        new AlertDialog.Builder(mContext, R.style.AppCompatAlertDialogStyle).setTitle("Warning").setMessage("You want to whiteList this application?").setPositiveButton("YES", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          //Adding items in Dataset 
          AllAppList appList = listStorage.get(position); 
          whiteListModel.setName(appList.getName()); 
          whiteListModel.setPackName(appList.getPackName()); 

          **whiteListModel.setIsSwitchOn(isChecked);** 

          if (existingDataSet != null) { 
           existingDataSet.add(whiteListModel); 
           saveScoreListToSharedpreference(existingDataSet); 
          } else { 
           newDataSet.add(whiteListModel); 
           saveScoreListToSharedpreference(newDataSet); 
          } 
          //Notifying adapter data has been changed..... 
          **listViewHolder.switchCompat.setChecked(whiteListModel.getIsSwitchOn());** 
          notifyDataSetChanged(); 

         } 
        }).setNegativeButton("No", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          **listViewHolder.switchCompat.setChecked(whiteListModel.getIsSwitchOn());** 
         } 
        }).show(); 
       }else { 
        **listViewHolder.switchCompat.setChecked(whiteListModel.getIsSwitchOn());** 
       } 
      } 
     }); 
     return convertView; 
    } 
+0

我didnot明白在哪里把我已经申报布尔在whiteList模型 – Raghav

+0

私人字符串名称; private String packName; private boolean isEnable; public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getPackName(){ return packName; } public void setPackName(String packName){ this。packName = packName; } public boolean isEnable(){ return isEnable; } public void setEnable(boolean enable){ isEnable = enable; } – Raghav

相关问题