【发布时间】:2017-07-10 07:05:23
【问题描述】:
我有一个包含 listview 的 Mainactivity,我还有另一个名为“WhiteList”的活动,它也包含 listview,我想将 MainActivity 中的 listview 项目添加到另一个活动的 Listview 项目。请帮我这样做
适配器代码:-
private LayoutInflater layoutInflater;
private List<AppList> listStorage;
private Context mContext;
int newarr_pos=0;
public AppAdapter(Context context, List<AppList> customizedListView) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listStorage = customizedListView;
this.mContext = context;
}
@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, 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());
listViewHolder.switchCompat.setTag(position);
listViewHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(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) {
//here i am adding items to another arraylist
List<WhiteListModel> res = new ArrayList<WhiteListModel>();
String name = listStorage.get(position).getName();
Drawable icon = listStorage.get(position).getIcon();
String packageName = listStorage.get(position).getPackName();
res.add(new WhiteListModel(name,icon,packageName));
listStorage.remove(position);
notifyDataSetChanged();
listViewHolder.switchCompat.setChecked(false);
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listViewHolder.switchCompat.setChecked(false);
}
}).show();
}
}
});
return convertView;
}
static class ViewHolder {
SwitchCompat switchCompat;
TextView textInListView;
ImageView imageInListView;
}
}
【问题讨论】:
-
为两个列表视图设置相同的适配器有什么问题'
-
你想在你的第二个活动中显示选中的项目吗?
-
我如何设置相同的适配器......
-
是的,我想在 android 的第二个活动中显示选中的项目
标签: android listview android-adapter