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

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

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


这将返回错误:不可转换的类型-无法将上下文转换为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;
    }

}

最佳答案

您可以创建对象,

AlarmList mContext=new AlarmList(getActivity());


然后像这样调用您的函数,

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

07-25 22:53