1

我有一个ListViewin a fragment,我可以从一个BroadcastReceiver. 但是,当应用程序从“最近”面板中删除时(滑动应用程序的缩略图 - 不要在设置中选择强制停止)BroadcastReceiver仍然运行(就像从最近删除应用程序时应该做的那样)但我当它尝试更新ListView.

我收集到的关于从最近删除应用程序时发生的情况是它不会杀死应用程序,它只会停止所有活动。这意味着BroadcastReceiversServices保持运行。这就是我的问题所在 - 我尝试更新Activity已停止的 ListView。

编辑:我认为从最近删除会导致 onStop() 被调用。

我是否需要创建一个更新 ListView 并保持活动运行的服务?会有什么不同吗?

我正在尝试做的类似于说一个短信应用程序。在 SMS 应用程序中,接收到广播并更新带有消息的 ListView 以显示新消息。

编辑:添加了一些代码

这是Fragment包含ListView

public class HistoryFragment extends FragmentBase implements OnItemAddedHandler {

    ListView lv;
    HistoryAdapter simpleAdpt;
    int mPosition;
    int index;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View histView = inflater.inflate(R.layout.history_fragment, container,
                false);

        setHasOptionsMenu(true); 


        ListView lv = (ListView) histView.findViewById(R.id.h_listView);


        simpleAdpt = new HistoryAdapter();

        lv.setAdapter(simpleAdpt);

        return histView;

    }

    private class HistoryAdapter extends BaseAdapter { 

        private List<Map<String, Object>> mPlanetsList;

        public HistoryAdapter() {
            mPlanetsList = DataModel.getInstance().getPlanetList();
        }

        @Override
        public int getCount() {
            return mPlanetsList.size();
        }

        @Override
        public Object getItem(int position) {
            return mPlanetsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (null == convertView) {
                convertView = LayoutInflater.from(getActivity()).inflate(
                        R.layout.history_item, null);
                Log.i("convertView", "was null");
            }

            TextView tv_title = (TextView) convertView
                    .findViewById(R.id.hi_tv_title); // This is part of the layout of each item

            HashMap<String, String> itemDataHashMap = (HashMap<String, String>) getItem(position);

            tv_title.setText(itemDataHashMap.get("planet"));

            return convertView;
        }
    }       

    @Override
    public void onItemAdded(Object data) {
        simpleAdpt.notifyDataSetChanged();
    }

    @Override
    public void onItemRemove(int postion) {
        simpleAdpt.notifyDataSetChanged();

    }

}

这是BroadcastReceiver我试图用来将项目添加到ListView. 它使用AlarmManager. 这意味着用户有时间在将项目添加到之前从最近面板中删除应用程序ListView

public class ReminderBroadcastReceiver extends BroadcastReceiver { 

    // This is declared in the manifest

    @Override
    public void onReceive(Context context, Intent intent) {

String title = "title";

    DataModel.getInstance()
        .addItem(title); // Add to History

    }
}

里面有DataModel

    public static DataModel getInstance() {
            if (null == instance) {
                Log.i("getInstance", "null");
                instance = new DataModel();
            }
            return instance;
        }

private DataModel() {
        initList();
    }

private void initList() {

        mHistoryList = History.getList();
        for (int i = 0; i < mHistoryList.size(); i++) {

            mPlanetsList.add(mHistoryList.get(i).createPlanet());

        }
    }

public void addItem(String title) {


        History history = new History();
        history.getDataHashMap().put("planet", title);  
        history.addToHistoryDB(); // This just adds to a Database

        mHistoryList.add(0, history); // Help keep the orders the same
        mPlanetsList.add(0, history.createPlanet());

        if (null != mOnItemAddHandler) {
            mOnItemAddHandler.onItemAdded(title);
        }
    }

如果需要更多代码,请说

4

0 回答 0