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

あなたはそれがリストビューには何も追加します示し、このコードには何もありません。 – CaseyB

+0

それは、私はどのようにわからないからです。私が言ったように、質問に。 – TheBlueCat

答えて

0

使用あなたのリスト上のArrayAdapter、その上にnotifyDatasetChangedを呼び出して、このアダプタにアイテムを追加し、ゾウのすべての

+0

彼らのAndroid開発者のページは、この方法がありますか? – TheBlueCat

0

まず私たちが使用してサーバーに接続しますネットワークスレッドの場合は、AsyncTaskまたはHandlersに移動してください。そのようなものを扱うのに特に使用されます。Thread

ListViewは、デフォルトのリストビューとカスタムリストビューを使用して作成することができます。カスタムリストビューでは、Row.xmlで独自の行デザインを設計でき、同じデザインがすべての行に使用されます。

先進的なやり方や高度なリストビューを希望さえすれば、異なる行に「n」個のデザインを使用することもできます。

ここでは、データを取得するためにAsyncTaskを使用し、行を表示するためにListviewを使用する必要があります。

下記のリンクから詳しい情報を得ることができます。

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

+0

私が言ったように、私はどのようにHttpGetからデータを追加するかではなく、リストビューを設計する方法を知っています。 – TheBlueCat

+0

すべてのアダプタは、データを渡すためにCollectionオブジェクトまたはString配列を渡す必要があります。カスタムアダプタを内部クラスとして記述し、CollectionオブジェクトまたはString配列をグローバルにします。その後、CollectionオブジェクトまたはArrayリストを更新し、アダプタobject.notifyDatasetChanged();を呼び出すだけです。これにより、自動的に新しいデータがリストに更新されます。 – Pavandroid

関連する問題