从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() {
        //此处省略了
    }
}

查看全部
g_zq_leaner
g_zq_leaner
2019/12/26 23:13
  • android-studio
  • android
  • 点赞
  • 收藏
  • 回答
    私信

1个回复