2011-08-23 68 views
2

我只是試圖從Web獲得一個簡單的JSON數組,格式如下:[「Country1」,「Country2」,「Country3」]然後使用該數組作爲在我的Android應用程序中的列表視圖。我沒有被困在如何使這個JSON數組,我只是困惑如何讓它進入應用程序中的列表視圖。Android應用程序:JSON數組從web到列表視圖

我已經嘗試了幾個不同的教程,但沒有一個使用與我一樣的佈局。

我的應用程序正在使用viewflipper在整個應用程序中始終保持基於Tabbased的佈局,因此沒有任何教程似乎與我的佈局一起工作。

任何幫助,非常感謝。

編輯:

下面是一些代碼,是的,我想從一個Web服務分析它,並在列表視圖中顯示。

public class Activity extends TabActivity implements OnClickListener { 

Button doSomething; 
TabHost tabHost; 
ViewFlipper flipper; 
ListView listview; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tablayout_1); 



    doSomething = (Button) findViewById(R.id.btn_do_something); 
    doSomething.setOnClickListener(this); 

    flipper = (ViewFlipper) findViewById(R.id.layout_tab_one); 

    listview = (ListView) findViewById(R.id.listview); 

    @SuppressWarnings("unchecked") 
ListAdapter adapter = new  ArrayAdapter(this,android.R.layout.simple_list_item_1,fetchTwitterPublicTimeline()); 


    //ListAdapter adapter = new SimpleAdapter(this, this.fetchTwitterPublicTimeline() , R.layout.main, new int[] { R.id.item_title, R.id.item_subtitle }); 


    listview.setAdapter(adapter); 

    flipper.setOnClickListener(this); 

    String tabname1 = getString(R.string.tabexample_tab1); 
    String tabname2 = getString(R.string.tabexample_tab2); 
    String tabname3 = getString(R.string.tabexample_tab3); 
    String tabname4 = getString(R.string.tabexample_tab4); 

    tabHost = getTabHost(); 
     tabHost.addTab(tabHost.newTabSpec("tab1").setContent(R.id.layout_tab_one).setIndicator(tabname1)); 
    tabHost.addTab(tabHost.newTabSpec("tab2").setContent(R.id.layout_tab_two).setIndicator(tabname2)); 
    tabHost.addTab(tabHost.newTabSpec("tab3").setContent(R.id.layout_tab_three).setIndicator(tabname3)); 
    tabHost.addTab(tabHost.newTabSpec("tab4").setContent(R.id.layout_tab_four).setIndicator(tabname4)); 

    tabHost.setCurrentTab(0); 




    listview.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
      flipper.showNext(); 


     }}); 




} 


public ArrayList<String> fetchTwitterPublicTimeline() 
{ 
    ArrayList<String> listItems = new ArrayList<String>(); 

    try { 
     URL twitter = new URL(
       "JSON.php"); 
     URLConnection tc = twitter.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       tc.getInputStream())); 

     String line; 
     while ((line = in.readLine()) != null) { 
      JSONArray ja = new JSONArray(line); 

      for (int i = 0; i < ja.length(); i++) { 
       JSONObject jo = (JSONObject) ja.get(i); 
       listItems.add(jo.getString("")); 
      } 
     } 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return listItems; 
} 


@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 

} 


} 

更新:我仍然無法得到它的工作,任何想法在下面的代碼有什麼錯?

public class Activity extends TabActivity implements OnClickListener { 

Button doSomething; 
TabHost tabHost; 
ViewFlipper flipper; 
ListView listview; 
HttpResponse re; 
String json; 
JSONObject j; 



@SuppressWarnings("deprecation") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    //final String TAG = "MainActivity"; 
    //final String URL = "JSON.php"; 

    super.onCreate(savedInstanceState); 



    setContentView(R.layout.tablayout_1); 



    final String[] listItems = new String[] { }; 



    /*======================================== 


// JSON object to hold the information, which is sent to the server 
    JSONObject jsonObjSend = new JSONObject(); 

    try { 
    // Add key/value pairs 
    jsonObjSend.put("key_1", "value_1"); 
    jsonObjSend.put("key_2", "value_2"); 

    // Add a nested JSONObject (e.g. for header information) 
    JSONObject header = new JSONObject(); 
    header.put("deviceType","Android"); // Device type 
    header.put("deviceVersion","2.0"); // Device OS version 
    header.put("language", "es-es"); // Language of the Android client 
    jsonObjSend.put("header", header); 

    // Output the JSON object we're sending to Logcat: 
    Log.i(TAG, jsonObjSend.toString(2)); 

    } catch (JSONException e) { 
    e.printStackTrace(); 
    } 

// Send the HttpPostRequest and receive a JSONObject in return 
      JSONObject jsonObjRecv = HTTPClient.SendHttpPost(URL, jsonObjSend); 
      String temp = jsonObjRecv.toString(); 


    /*==============================================*/ 



    doSomething = (Button) findViewById(R.id.btn_do_something); 
    doSomething.setOnClickListener(this); 

    flipper = (ViewFlipper) findViewById(R.id.layout_tab_one); 

    listview = (ListView) findViewById(R.id.listview); 







    /* try { 

      JSONArray array = jsonObjRecv.getJSONArray(""); //(JSONArray) new JSONTokener(json).nextValue(); 

      String[] stringarray = new String[array.length()]; 
      for (int i = 0; i < array.length(); i++) { 
       stringarray[i] = array.getString(i); 
      } 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringarray); 
      listview.setAdapter(adapter); 
    } catch (JSONException e) { 
      // handle JSON parsing exceptions... 
    }*/ 


    //@SuppressWarnings("unchecked") 
    ListAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,fetchTwitterPublicTimeline()); 


    //ListAdapter adapter = new SimpleAdapter(this, this.fetchTwitterPublicTimeline() , R.layout.main, new int[] { R.id.item_title, R.id.item_subtitle }); 


    listview.setAdapter(adapter); 

    flipper.setOnClickListener(this); 

    String tabname1 = getString(R.string.tabexample_tab1); 
    String tabname2 = getString(R.string.tabexample_tab2); 
    String tabname3 = getString(R.string.tabexample_tab3); 
    String tabname4 = getString(R.string.tabexample_tab4); 

    tabHost = getTabHost(); 
    tabHost.addTab(tabHost.newTabSpec("tab1").setContent(R.id.layout_tab_one).setIndicator(tabname1)); 
    tabHost.addTab(tabHost.newTabSpec("tab2").setContent(R.id.layout_tab_two).setIndicator(tabname2)); 
    tabHost.addTab(tabHost.newTabSpec("tab3").setContent(R.id.layout_tab_three).setIndicator(tabname3)); 
    tabHost.addTab(tabHost.newTabSpec("tab4").setContent(R.id.layout_tab_four).setIndicator(tabname4)); 

    tabHost.setCurrentTab(0); 




    listview.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
      flipper.showNext(); 


     }}); 




} 



public ArrayList<String> fetchTwitterPublicTimeline() 
{ 
    ArrayList<String> listItems = new ArrayList<String>(); 

    try { 
     URL twitter = new URL(
       "JSON.php"); 
     URLConnection tc = twitter.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       tc.getInputStream())); 


     String line = null; 

     //make sure youe String line is completely filled after that.. 
     if (!line.equals(null) && !line.equals("") && line.startsWith("[")) 
     { 
     JSONArray jArray = new JSONArray(line); 
     for (int i = 0; i < jArray.length(); i++) 
     { 
      JSONObject jobj = jArray.getJSONObject(i); 
     // also make sure you get the value from the jsonObject using some key 
     // like, jobj.getString("country"); 

     listItems.add(jobj.getString("")); 
     } 
      } 



    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return listItems; 
} 



    /* public ArrayList<String> fetchTwitterPublicTimeline() 
{ 
    ArrayList<String> listItems = new ArrayList<String>(); 

    try { 
     URL twitter = new URL(
       "JSON.php"); 
     URLConnection tc = twitter.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       tc.getInputStream())); 



     //make sure youe String line is completely filled after that.. 
     if (!line.equals(null) && !line.equals("") && line.startsWith("[")) 
     { 
     JSONArray jArray = new JSONArray(line); 
     for (int i = 0; i < jArray.length(); i++) 
     { 
      JSONObject jobj = jArray.getJSONObject(i); 
     // also make sure you get the value from the jsonObject using some key 
     // like, jobj.getString("country"); 

     listItems.add(jobj.getString("")); 
     } 
      } 




     /* String line; 
     while ((line = in.readLine()) != null) { 
      JSONArray ja = new JSONArray(line); 

      for (int i = 0; i < ja.length(); i++) { 
       JSONObject jo = (JSONObject) ja.get(i); 
       listItems.add(jo.getString("")); 
      } 
     } 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return listItems; 
}*/ 


@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 

} 


} 

更新,這裏有jArray的值所報告的logcat:

08-26 16:49:07.246:VERBOSE /應用(472):jarray值:[ 「國家1」,「COUNTRY2 「,」Country3「]

這些都是正確的值!

+0

?你應該提供一些代碼。 – user370305

回答

7

這個工作在我剛剛創建了一個簡單的測試應用程序...

ListView list = (ListView) findViewById(...); 
String json = "[\"Country1\",\"Country2\",\"Country3\"]"; 
try { 
     JSONArray array = (JSONArray) new JSONTokener(json).nextValue(); 

     String[] stringarray = new String[array.length()]; 
     for (int i = 0; i < array.length(); i++) { 
      stringarray[i] = array.getString(i); 
     } 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringarray); 
     list.setAdapter(adapter); 
} catch (JSONException e) { 
     // handle JSON parsing exceptions... 
} 
你想創建一個JSON數組或要分析它在列表視圖顯示
+0

這工作完美,它只是一個問題,現在從Web服務器上的PHP獲取JSON字符串,任何想法? – EI756

相關問題