2012-04-29 90 views
0
package com.database_demo; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.net.ParseException; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.Toast; 

public class Database_demo extends ListActivity { 

    /** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    String result = null; 
    InputStream is = null; 
    StringBuilder sb = null; 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    List<String> r = new ArrayList<String>(); 

    try{ 

    //http post 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://10.0.0.4/WWW/test.php"); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity(); 
    is = entity.getContent(); 
    } 
    catch(Exception e){ 
     Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show(); 
    } 

    //Convert response to string 
    try 
    { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); 

     sb = new StringBuilder(); 

     String line = null; 

     while ((line = reader.readLine()) != null) 
     { 
     sb.append(line + "\n"); 
     } 

     is.close(); 

     result = sb.toString(); 
    } 
    catch(Exception e) 
    { 
     Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show(); 
    } 
    //END Convert response to string 
    try{ 
      JSONArray jArray = new JSONArray(result); 
      JSONObject json_data=null; 
      for(int i=0;i<jArray.length();i++) 
      { 
       json_data = jArray.getJSONObject(i); 
       r.add("E-mail adres: \n " + json_data.getString("E-mail")); 
       r.add("Wachtwoord: \n" + json_data.getString("Wachtwoord")); 

      } 
      setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, r)); 
     } 
     catch(JSONException e1){ 
      Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show(); 
     } catch (ParseException e1) { 
      Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show(); 
    } 

    } 
} 

我表現出我的擴展列表兩件事 「r.add(」 電子郵件住址:\ n 「+ json_data.getString(」 電子郵件 「)); 」 和「r .add(「Wachtwoord:\ n」+ json_data.getString(「Wachtwoord」));. 現在我想要r.add(「E-mail adres:\ n」+ json_data.getString(「E-mail」)) );將變成紅色在我的擴展列表中顯示我怎麼做任何建議Android的可擴展列表顏色

回答

0

可以使用SpannableStringBuilder代替StringBuilder和應用TextAppearanceSpan到‘你spannabl的電子郵件’部分? e字符串。