2012-03-22 82 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

相关问题