如何从 android 中的活动切换在列表视图适配器中定义哪个?



我有一个应用程序,其中包含在列表视图适配器中定义的开关兼容的列表视图。我想在包含列表视图的活动中启用/禁用切换方法。我该怎么做。

适配器代码:-

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
}
}
}

通过模型类执行此操作。

项目.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;
}
}

主活动.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();
}

您的适配器.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.
}
}

我的项目.java与您的AllApplist.java相同,只需在其中放置一个布尔值,然后按照代码中的操作进行操作。

最新更新