从activity返回到fragment时listview不显示

Fragment代码如下:


public class TodolistFragment extends Fragment {

    private List<Todo> todos = new ArrayList<>();
    //NotificationManager notificationManager;
    SimpleAdapter adapter;
    ListView listView;
    View view;
    Button button;
    private int uid;
    private String date;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        uid = ((HomePage)context).user_id;
        date = ((HomePage)context).date;
        //buttonClickListener = (OnItemButtonClickListener) context;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        button = (Button)getActivity().findViewById(R.id.add_list);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                showDialog();
            }
        });
        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.CART_BROADCAST");
        BroadcastReceiver mItemViewListClickReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String msg = intent.getStringExtra("data");
                if("refresh".equals(msg))
                    init();
            }
        };
        broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_todolist_fragment, container, false);
        init();
        listView = (ListView)view.findViewById(R.id.list);
        return view;
    }
    /*public interface OnItemButtonClickListener{
        void onItemViewClick(View v);
    }*/

    @Override
    public void onResume(){
        System.out.println("todolist onresume!");
        super.onResume();
        //init();
    }

    private void init(){
        listView = (ListView)view.findViewById(R.id.list);
        MySqliteDB mydb = new MySqliteDB(view.getContext(),"MyDB", null, 1);
        SQLiteDatabase db = mydb.getReadableDatabase();
        Cursor cursor;
        cursor = db.query("todolist",new String[]{"list_id","title","total_time","complete_time"},"user_id="+uid,null,null,null,null);
        ArrayList<Map<String, String>>  list = new ArrayList<Map<String, String>>();
        while (cursor.moveToNext()) {
            //数据存入map
            Map<String, String> map = new HashMap<String, String>();
            final int t = cursor.getInt(cursor.getColumnIndex("total_time"))-cursor.getInt(cursor.getColumnIndex("complete_time"));
            if(t > 0) {
                final int list_id = cursor.getInt(cursor.getColumnIndex("list_id"));
                map.put("title", cursor.getString(cursor.getColumnIndex("title")));
                map.put("time", t + "分钟");
                //map添加到集合list中
                list.add(map);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putInt("list_id", list_id);
                        bundle.putInt("user_id", uid);
                        bundle.putInt("time", t);
                        bundle.putString("date", date);
                        intent.putExtras(bundle);
                        intent.setClass(getActivity(), Timing.class);
                        getActivity().startActivity(intent);
                    }
                });
            }
        }
        adapter=new SimpleAdapter(getContext(),list,R.layout.todolist_item,
                new String[]{"title","time"},new int[]{R.id.todolist_title,R.id.todolist_time});
        listView.setAdapter(adapter);
    }

    private void showDialog() {
        //此处省略了
    }
}

1个回答

g_zq_leaner
g_zq_leaner 我看到您的这篇博客了,但是我尝试在Resume函数中调用init函数,init函数重新为listview绑定了adapter,这样也不可以,是什么问题呢?
大约 2 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问