调用notifyDataSetChanged()onResume()时,适配器为null

时间:2014-01-24 16:05:38

标签: android android-fragments adapter

我有一个片段,其中包含我想在添加新记录时更新的listView。我试图在片段中的onResume()中调用.notifyDataSetChanged(),但适配器= null。

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

    exmaplePrefs = this.getActivity().getSharedPreferences(PREFS, 0);
    json = exmaplePrefs.getString("jsonString", "cant find json");

    JSONObject MainObj = null;
    JSONArray JsonArray = null;

    try {

        MainObj = new JSONObject(json);
        JsonArray = MainObj.getJSONArray("Events");

            for(int i = 0; i < JsonArray.length(); i++){
                JSONObject c = JsonArray.getJSONObject(i);

                // Storing  JSON item in a Variable
               String time = c.getString("time");
               String event = c.getString("event");
               String player = c.getString("player");   

               String from[] = {"time","event", "player"};
               int to[] = {R.id.time,R.id.event, R.id.player};

                // Adding value HashMap key => value
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("time", time);
                map.put("event", event);
                map.put("player",player);
                newItemlist.add(map);
                listV=(ListView)rootView.findViewById(android.R.id.list);
                adapter = new SimpleAdapter(getActivity(), newItemlist, R.layout.custom_list_row, from, to);
                listV.setAdapter(adapter);
            }

    } catch (Exception e) {
        // TODO: handle exception
        System.out.print("ERROR");
    }

    return rootView;
}
@Override
public void onResume(){
    super.onResume();
    System.out.println("worked");

    if (adapter != null){
        adapter.notifyDataSetChanged();     
    }
}

我认为这与在onCreateView()上创建的适配器有关,当我返回Fragment时不可见。

2 个答案:

答案 0 :(得分:1)

如果抛出Exception,您将直接移至catch块,从而跳过adapter的初始化。这可能意味着解析您的JSON字符串时出错。

答案 1 :(得分:1)

看着这个,我看到了一些问题。

  1. catch ( Exception ex)的习惯通常很差。尝试仅捕获您担心的特定异常,这会使修复问题变得更容易。
  2. 为什么在for循环中创建了ArrayAdapter?你可能想要这样的东西:

    ArrayList list=new ArrayList(); //Specify what type of value is in the ArrayAdapter
    adapter = new SimpleAdapter(getActivity(), newItemlist, R.layout.custom_list_row,list);
    listV.setAdapter(adapter);
    MainObj = new JSONObject(json);
    JsonArray = MainObj.getJSONArray("Events");
    
    for(int i = 0; i < JsonArray.length(); i++){
        JSONObject c = JsonArray.getJSONObject(i);
    
        // Storing  JSON item in a Variable
       String time = c.getString("time");
       String event = c.getString("event");
       String player = c.getString("player");   
    
       String from[] = {"time","event", "player"};
       int to[] = {R.id.time,R.id.event, R.id.player};
    
        // Adding value HashMap key => value
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("time", time);
        map.put("event", event);
        map.put("player",player);
        newItemlist.add(map);
        listV=(ListView)rootView.findViewById(android.R.id.list);
        adapter.add(map);
    }
    
  3. 我不太确定SimpleArrayAdapter是什么,但你想在循环之外创建一个新的,并且最有可能在循环内部添加内容,重新创建它似乎是一个糟糕的选择。您可能需要特别使用adapter.add(map)命令进行操作,但这种一般模式应该有所帮助。您还需要将ArrayList list更改为ArrayList<String> list=new ArrayList<String>(),以及ArrayAdapter存储的内容(看起来像HashMap<String,String>