2017-07-21 7 views
0

私はlistviewアダプタで定義するswitch compatを持つlistviewを含むアプリケーションを持っています。私は、リストビューを含むアクティビティのメソッドを有効/無効にしたい。それ、どうやったら出来るの。アダプタのどのようにアンドロイドの活動から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

私はあなたの質問から理解しました:あなたはtextViewとスイッチとしてitemViewを持っているlistViewを持っていますあなたのリストビューのスイッチを変更したときに、その変更をあなたの活動に反映させたいとします。つまり、アダプターのアイテム内の何かを変更すると、そのアダプターが接続されているアクティビティーにその変更を通知する必要があります。そうですか? – Ashwani

+0

いいえアクティビティにあるメソッドからスイッチの状態を変更したい。 – Raghav

+0

swicthはAdapterクラスにあります – Raghav

答えて

0

モデルクラスを経由して、それを実行してください。

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と同じです。ブール値を入れてコード内で行われるようにしてください。

関連する問題