我制作了一个自定义对话框接口(定义到一个单独的片段中),如果需要,可以从片段中调用。这个对话框接口扩展了一个listView,所以我称之为自定义列表适配器(扩展BaseAdapter)。现在,我想引用自定义列表适配器中对话框片段中定义的全局变量。我该怎么做?
编辑
我想用这个自定义布局实现一个对话框片段:
****DIALOG TITLE****
ListView where each row have this layout:
ICON - SOME TEXT - SwitchWidget
我有这个片段:
public class FilterDialogFragment extends DialogFragment {
//Checked is a class that store some info
public ArrayList<Checked> mSelectedItem; <--- variable i want to modify
public static FilterDialogFragment newInstance(ArrayList<Checked> checked) {
FilterDialogFragment fragment = new FilterDialogFragment();
Bundle args = new Bundle();
args.putParcelableArrayList("ARRAYLIST_STRING", checked);
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mSelectedItem = getArguments().getParcelableArrayList("ARRAYLIST_STRING");
String[] checkItem = new String[mSelectedItem.size()];
boolean[] isChecked = new boolean[mSelectedItem.size()];
for(int i=0; i<mSelectedItem.size(); i++) {
checkItem[i]=""+mSelectedItem.get(i).getWhatChecked();
if(mSelectedItem.get(i).getShow()==1) {
//show
isChecked[i]=true;
} else {
isChecked[i]=false;
}
}
final Dialog dialog = new Dialog(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_layout, null);
ListView lv = (ListView) view.findViewById(R.id.custom_list);
Button ok_button = (Button) view.findViewById(R.id.ok_button);
ok_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, getActivity().getIntent());
dialog.dismiss();
}
});
CustomListAdapterDialog mAdapter = new CustomListAdapterDialog(getActivity());
mAdapter.addItem(new DialogModel(R.color.green, checkItem[i], isChecked[i]));
lv.setAdapter(mAdapter);
dialog.setContentView(view);
return dialog;
}
这是我的自定义适配器:
public class CustomListAdapterDialog extends BaseAdapter {
[....]
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
int type = getItemViewType(position);
//System.out.println("getView " + position + " " + convertView + " type = " + type);
if(convertView == null) {
//initialize the view Holder
viewHolder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = inflater.inflate(R.layout.dialog_row_layout, null);
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.marker);
viewHolder.tvNomeChecked = (TextView) convertView.findViewById(R.id.nome_marker);
viewHolder.swCheck = (Switch) convertView.findViewById(R.id.checked_item);
break;
case TYPE_SEPARATOR:
convertView = inflater.inflate(R.layout.dialog_section_layout, null);
viewHolder.tvSeparator = (TextView) convertView.findViewById(R.id.section_alert_dialog);
break;
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final DialogModel model;
switch (type) {
case TYPE_ITEM:
model =(DialogModel) mData.get(position);
//viewHolder.ivIcon.setImageDrawable(model.getMarkerIcon());
viewHolder.ivIcon.setBackgroundResource(model.getMarkerColor());
viewHolder.tvNomeChecked.setText(model.getNomePercorso());
if(model.isCkeck()) {
viewHolder.swCheck.setChecked(true);
} else {
viewHolder.swCheck.setChecked(false);
}
viewHolder.swCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//HERE I WANT TO CALL THE mSelectedItem OF MY FilterDialogFragment
}
});
break;
case TYPE_SEPARATOR:
model =(DialogModel) mData.get(position);
viewHolder.tvSeparator.setText(model.getNomeSection() );
break;
}
return convertView;
}
private static class ViewHolder {
ImageView ivIcon;
TextView tvNomeChecked;
TextView tvSeparator;
Switch swCheck;
}
对话框和片段之间是否存在任何直接/间接关系?如果是,则使用接口传递变量,如果不是,则可以使用静态变量,但尽量避免使用静态变量/类,并更改代码以避免使用静态关键字创建接口并在对话框片段中实现它,然后将此引用传递给适配器并在适配器中初始化它,然后使用