如何从android中的活动切换到listview适配器中的哪个定义?

问题描述:

我有一个应用程序,其中包含listview适配器中定义的开关compat listview。我想要启用/禁用在包含listview的活动中切换一个方法。我怎么做。适配器的如何从android中的活动切换到listview适配器中的哪个定义?

代码: -

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) { 

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

         //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(); 

      } 

     } 
    }); 

列表视图中点击代码: -

private List<AllAppList> getInstalledApps() { 
    List<AllAppList> res = new ArrayList<AllAppList>(); 
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0); 
    for (int i = 0; i < packs.size(); i++) { 
     PackageInfo p = packs.get(i); 
     if ((!isSystemPackage(p))) { 
      if (p.applicationInfo.packageName.equalsIgnoreCase("com.soopermo.batterybooster")){ 
       continue; 
      } 
      boolean isWhiteList = false; 
      if (whiteListModels != null) { 
       for (int j = 0; j < whiteListModels.size(); j++) { 
        model = whiteListModels.get(j); 
        Log.e(TAG, "p*****" + model.getPackName()); 
        if (p.applicationInfo.packageName.equalsIgnoreCase(model.getPackName())) { 
         // This package is whitlist package 
         isWhiteList = true; 
         //Here is want to enable/disable switch 

        } 
       } 
      } 
+0

我从你的问题中了解到:你有一个listView,它具有itemView作为textView和一个开关,在列表视图中更改切换时,您想要反映您的活动中的变化。即你改变你的项目在适配器中的东西,它应该通知该更改为该适配器所连接的活动。是这样吗? – Ashwani

+0

不,我想从活动中的方法更改开关状态。 – Raghav

+0

swicth在适配器类 – Raghav

通过模型类做它。

Item.java

public class Item{ 
    private String appName; 
    private boolean isEnable; 
    public String getAppName(){ 
     return this.appName; 
    } 
    public void setAppName(String name){ 
     this.appName = name; 
    } 
    public boolean isEnabled(){ 
     return this.isEnabled; 
    } 
    public void setEnabled(boolean value){ 
     this.isEnabled = value; 
    } 
} 

MainActivity.java

List<Item> myItems = new ArrayList<>(); 
... 
..onCreate(Bundle savedInstance){ 
...//your code 
YourAdapter adapter = new YourAdapter(MainActivity.this, myItems);//pass the list of items. 
yourListView.setAdapter(adapter); 

//your action call when you want to trigger the change suppose 2nd item has to be enabled. 
myItems.get(1).setEnabled(true); 

//and after that notify this change to your adapter 
adapter.notifyDataSetChanged(); 

} 

YourAdapter.java

List<Items> itemList; 
Context context; 

public YourAdapter(Context context, List<Items> itemList){ 
    this.context = context; 
    this. itemList = itemList; 
} 

public View getView(int position, View convertView, ViewGroup parent){ 
    ...//you code to inflate your view 
    Items item = itemList.get(position); 

    if(item.isEnabled()){ 
     //toggle your switch. 
    } 

} 

我Item.java是与你的AllApplist.java相同,只需在其中添加一个布尔值,并在代码中完成它。