从自定义BaseAdapter调用片段中的函数

时间:2015-09-29 12:37:31

标签: java android android-fragments baseadapter

我有一个包含三个片段的片段活动。其中一个片段(AlarmList)包含一个自定义列表(AlarmListAdapter)。列表中的每个项目都有一个删除按钮。按下此删除按钮时,应调用AlarmList片段中的删除功能(deleteAlarm);从内部数据库中删除警报,并通过调用Adapter.NotifyDataSetChanged()将其从列表中删除。

我的问题出在我调用的AlarmListAdapter中:

((AlarmList)mContext).deleteAlarm(((Long) view.getTag()).longValue());

返回错误:Inconvertible types - 无法将上下文强制转换为AlarmList

如何从我的自定义适配器调用片段AlarmList中的函数?

以下是完整代码。谢谢!

AlarmList.java

public class AlarmList extends Fragment {

    private AlarmListAdapter mAdapter;
    private AlarmDBHelper dbHelper;

    ListView list;

    private Context mContext;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_alarm_list, container, false);

        mContext = getActivity();
        dbHelper = new AlarmDBHelper(mContext);

        mAdapter = new AlarmListAdapter(mContext, dbHelper.getAlarms());

        list = (ListView)rootView.findViewById(R.id.list);
        list.setAdapter(mAdapter);

        list.setDivider(new ColorDrawable(R.color.white));
        list.setDividerHeight(1);

        return rootView;
    }

    public void setAlarmEnabled(long id, boolean isEnabled) {
        AlarmManagerHelper.cancelAlarms(mContext);
        AlarmModel model = dbHelper.getAlarm(id);
        model.isEnabled = isEnabled;
        dbHelper.updateAlarm(model);

        AlarmManagerHelper.setAlarms(mContext);
    }

    public void startAlarmDetailsActivity(long id) {
        Intent intent = new Intent(mContext, AlarmDetails.class);
        intent.putExtra("id", id);
        startActivityForResult(intent, 0);
    }

    public void deleteAlarm(long id) {
        final long alarmId = id;

        AlarmManagerHelper.cancelAlarms(mContext);
        //Delete alarm from DB by id
        dbHelper.deleteAlarm(alarmId);
        //Refresh the list of the alarms in the adaptor
        mAdapter.setAlarms(dbHelper.getAlarms());
        //Notify the adapter the data has changed
        mAdapter.notifyDataSetChanged();
        //Set the alarms
        AlarmManagerHelper.setAlarms(mContext);
    }
}

AlarmListAdapter.java

public class AlarmListAdapter extends BaseAdapter {

    private Context mContext;
    private List<AlarmModel> mAlarms;

    public AlarmListAdapter(Context context, List<AlarmModel> alarms) {
        this.mContext = context;
        this.mAlarms = alarms;

    }

    public void setAlarms(List<AlarmModel> alarms) {
        mAlarms = alarms;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.alarm_list_item, parent, false);
        }

        final AlarmModel model = (AlarmModel) getItem(position);

        TextView txtTime = (TextView) view.findViewById(R.id.alarm_item_time);
        txtTime.setText(String.format("%02d:%02d", model.timeHour, model.timeMinute));

        TextView txtName = (TextView) view.findViewById(R.id.alarm_item_name);
        txtName.setText(model.name);

        ToggleButton btnToggle = (ToggleButton) view.findViewById(R.id.alarm_item_toggle);
        btnToggle.setChecked(model.isEnabled);
        btnToggle.setTag(Long.valueOf(model.id));
        btnToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ((AlarmList)mContext).setAlarmEnabled(((Long) buttonView.getTag()).longValue(), isChecked);
            }
        });

        view.setTag(Long.valueOf(model.id));
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                ((AlarmList)mContext).startAlarmDetailsActivity(((Long) view.getTag()).longValue());
            }
        });

        ImageView deleteButton = (ImageView)view.findViewById(R.id.delete_icon);
        deleteButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                ((AlarmList)mContext).deleteAlarm(((Long) view.getTag()).longValue());
            }
        });

        return view;
    }

}

1 个答案:

答案 0 :(得分:1)

您可以创建像

这样的对象
example

然后调用你的函数,

AlarmList mContext=new AlarmList(getActivity());