2012-03-22 84 views
0

我仍然在努力尋找我的問題的答案。我想爲列表視圖中的每個項目下載3個字符串給手機。我知道如何從服務器獲取所有數據,而不是如何將數據附加到litview,我真的很煩,這個問題拖累我。如何在互聯網上添加列表視圖項目

我的代碼:

public class ChatService extends Activity { 
    /** Called when the activity is first created. */ 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.chatservice); 
     try { 
      ContactsandIm(); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (URISyntaxException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     CheckLogin(); 


    private void CheckLogin() { 
     // TODO Auto-generated method stub 
     // Create a new HttpClient and Post Header 
       HttpClient httpclient = new DefaultHttpClient(); 

       /* login.php returns true if username and password is equal to saranga */ 
       HttpPost httppost = new HttpPost("http://gta5news.com/login.php"); 

       try { 

        // Execute HTTP Post Request 
        Log.w("HttpPost", "Execute HTTP Post Request"); 
        HttpResponse response = httpclient.execute(httppost); 

        String str = inputStreamToString(response.getEntity().getContent()) 
          .toString(); 
        Log.w("HttpPost", str); 

        if (str.toString().equalsIgnoreCase("true")) { 
         Log.w("HttpPost", "TRUE"); 
         try {Thread.sleep(250); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 

         //put intent here(21/3/12); 

        } else { 
         Log.w("HttpPost", "FALSE"); 

        } 

       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

      private StringBuilder inputStreamToString(InputStream is) { 
       String line = ""; 
       StringBuilder total = new StringBuilder(); 
       // Wrap a BufferedReader around the InputStream 
       BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
       // Read response until the end 
       try { 
        while ((line = rd.readLine()) != null) { 
         total.append(line); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       // Return full string 
       return total; 
      } 




    private void ContactsandIm() throws URISyntaxException, ClientProtocolException, IOException { 
     // TODO Auto-generated method stub 
     BufferedReader in = null; 
     String data = null; 


     HttpClient get = new DefaultHttpClient(); 
     URI website = new URI("http://www.gta5news.com/test.php"); 
     HttpGet webget = new HttpGet(); 
     webget.setURI(website); 
     HttpResponse response = get.execute(webget); 
     Log.w("HttpPost", "Execute HTTP Post Request"); 
     in = new BufferedReader (new InputStreamReader(response.getEntity().getContent())); 
     //now we'll return the data that the PHP set from the MySQL Database. 




     if (in.equals("True")); { 
      Toast.makeText(this,"yay", Toast.LENGTH_LONG).show(); 
     } 






    } 

    // end bracket for "ContactsandIm" 

    private void showToast(String message) { 
     Toast.makeText(this, message, Toast.LENGTH_LONG).show(); 
    } 
} 
+1

沒有什麼在此代碼,您顯示,會添加任何東西到ListView。 – CaseyB 2012-03-22 17:24:51

+0

那是因爲我不知道如何。正如我所說,在這個問題上。 – TheBlueCat 2012-03-22 17:36:31

回答

0

使用清單上的ArrayAdapter,並將項目添加到該適配器,然後調用notifyDatasetChanged它,鄒

+0

是他們的一個android開發者頁面,那有這個方法嗎? – TheBlueCat 2012-03-22 17:28:20

0

首先,當我們使用連接到服務器網絡線程,那麼你應該去AsyncTaskHandlers。這是專門用於處理這種Thread s。

ListView可以通過使用默認的ListView和自定義列表視圖來創建,我們可以在Row.xml中設計自己的Row設計,同樣的設計將用於所有行。

如果您想要前進或進入某些高級列表視圖,那麼即使我們可以對不同的行使用'n'個設計。

在你的情況下,你應該使用AsyncTask來獲取數據並使用Listview來顯示行。

您可以從下面的鏈接中獲得更多信息。

https://docs.google.com/leaf?id=0B2qCFbmeiTFxN2ViZjVlOTUtNmY3ZS00NThhLTg3N2UtYjVkYjgyM2Y4MWUy&hl=en&authkey=COeP8JYN

+0

正如我所說,我知道如何設計listviews,而不是如何從HttpGet追加數據。 – TheBlueCat 2012-03-22 19:12:36

+0

每個適配器我們都應該給一個Collection對象或者String數組來傳遞數據。將您的自定義適配器編寫爲內部類並使Collection對象或字符串數​​組成爲全局。然後,在你想要更新Collection對象或數組列表並只調用適配器object.notifyDatasetChanged();之後。這會自動將新數據更新到列表中。 – Pavandroid 2012-03-23 01:47:15

相關問題