2013-07-24 82 views
2

我是新來的android開發。 JSON在使用主要功能時沒有問題,但在將代碼移動到ListFragment時出現問題Android JSON在ListFragment

此行沒有任何事情發生: JSONObject json = userFunctions.announcementFeed(myID,myType);

下面是完整的代碼:

package com.example.demo123; 
import java.util.ArrayList; 
import java.util.HashMap; 
import library.UserFunctions; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.os.Bundle; 
import android.support.v4.app.ListFragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.SimpleAdapter; 

public class AnnouncementList extends ListFragment { 
private static final String TAG = "MyAnnouncement"; 

JSONArray announcementList = null;    
UserFunctions userFunctions; 

String myID = MainActivity.myID; 
String myType = MainActivity.myType; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {  
    return super.onCreateView(inflater, container, savedInstanceState); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    Log.i(TAG,"START ANNOUNCEMENT LIST"); 

    ArrayList<HashMap<String, String>> theAnnnouncement = new ArrayList<HashMap<String, String>>(); 
    JSONObject json = userFunctions.announcementFeed(myID, myType); 

    try { 
     if (json.getString("success") != null) { 
      announcementList = json.getJSONArray("announceDT"); 
      for (int i = 0; i < announcementList.length(); i++) { 
       JSONObject c = announcementList.getJSONObject(i); 
       String title = c.getString("topic"); 
       String course = c.getString("course") + " - " + c.getString("courseTitle"); 
       String created = "By " + c.getString("entered_by") + " | " + c.getString("entry_date"); 

       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put("title", title); 
       map.put("course", course); 
       map.put("created", created); 

       theAnnnouncement.add(map); 
      } 
     }     
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    String[] from = { "title","course","created" }; 
    int[] to = { R.id.title,R.id.course,R.id.created}; 

    SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), theAnnnouncement, R.layout.list_item, from, to);    
    setListAdapter(adapter);   
} 

}

+0

LogCat是什麼意思?你的announcementFeed()函數應該做什麼?任何網絡?更多的細節將有助於 –

+0

@JadeByfield你可以在這裏查看logcat http://www.letmecode.com/tmp/log.txt – iQbal

+0

@JadeByfield for announcementTeed():http://www.letmecode.com/tmp /announcement.txt – iQbal

回答

1

爲什麼是絕對有必要把這種邏輯在onActivityCreated?你的片段在這個時候不會被完全初始化,所以你可能想把這個邏輯移動到onResume或其他地方。

編輯:我看了一下你鏈接的代碼,以及announcementFeed()方法。

看起來你正試圖在主線程上做一些聯網工作,

public JSONObject announcementFeed(String username, String userType){ 
    .................................. 

    JSONObject json = jsonParser.getJSONFromUrl(loginURL, params); 
    // return json 
    return json; 
} 

您在onActivityCreated中調用此方法。我建議將其移至後臺線程或AsyncTask。儘管如此,沒有LogCat,我不能100%確定發生了什麼,但至少我們能夠指出這一點。

+0

好的謝謝!我會嘗試AsyncTask。順便說一句,這裏是logcat http://www.letmecode.com/tmp/log.txt – iQbal