2012-11-24 2 views
-1
public ArrayList<String> getData() 
    { 

     ArrayList<String> listItems = new ArrayList<String>(); 

     String result = ""; 
     InputStream isr=null; 

     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://10.0.2.2/get_data.php"); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
       isr = entity.getContent(); 
    } 
    catch(Exception e){ 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
      //resultView.setText("Couldnt connect to database"); 
    } 

     try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso- 8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
      } 
      isr.close(); 

      result=sb.toString(); 
    } 
     catch(Exception e){ 
      Log.e("log_tag", "Error converting result "+e.toString()); 
     } 
      //parse json data 
      try { 
       String s = ""; 
       JSONArray jArray = new JSONArray(result); 


       for(int i=0; i<jArray.length();i++) 
       { 
        JSONObject json=jArray.getJSONObject(i); 


       listItems.add(json.getString("name")); 
       listItems.add(json.getString("ph.no")); 
       listItems.add(json.getString("id")); 
        // adding HashList to ArrayList 

       } 


       // resultView.setText(s); 

      } catch (Exception e) 
      { 
      // TODO: handle exception 
       Log.e("log_tag", "Error Parsing Data "+e.toString()); 
      } 


      return listItems; 

      // selecting single ListView item 


} 

전화 번호부라는 안드로이드 응용 프로그램을 개발 중입니다. json 객체로서 sever로부터 사람 의 상세를 추출하고 있습니다. android의 세부 정보 표시Json 객체가 목록보기에 표시됩니다.

다른 사람의 연락처 정보를 다른 행에 표시 할 수 있습니다. 동일한 행에있는 한 사람의 세부 정보를 표시하는 방법

+0

당신은 사용자의 ListView를 사용해야합니다. –

+0

[BaseAdapter] (http://developer.android.com/reference/android/widget/BaseAdapter.html)를 사용하여 목록보기로 데이터로드 [example] (http://anujarosha.wordpress.com/2011/11)/30/how-to-use-baseadapter 및 onitemclicklistener-in-android /) –

답변

0

사용자 정의 ArrayAdapter로 Android ListView 항목을 사용자 정의 할 수 있습니다. 이것에 대한 당신은이 방법을 수행 할 수 있습니다

,

/* In main activity */ 
ListView myListView = (ListView)findViewById(R.id.myListView); 
final ArrayList<Phonebook> todoItems = new ArrayList<Phonebook>(); 
final YourAdapter x= new YourAdapter(this,R.layout.your_listviewlayout,todoItems); 
myListView.setAdapter(x); 

Phonebook phnbk= new Phonebook(); 
// enter code for set values to Phonebook class variables 
/* Inserting the values to array */ 
todoItems.add(phnbk); 

/* Customized array adaptor class */ 
private class YourAdapter extends ArrayAdapter<Phonebook> 
{ 
    private ArrayList<Phonebook> items; 
    public YourAdapter (Context context, int textViewResourceId, ArrayList<Phonebook> items) 
    { 
     super(context, textViewResourceId, items); 
     this.items = items; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     View v = convertView; 
     if (v == null) 
     { 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.your_listviewlayout, null); 
     } 
     Phonebook o = items.get(position); 
     if (o != null) 
     { 
      //insert values to each text view in your list view layout 
      tv1.setText(o.name); 
      tv2.setText(o.phnnum); 
         tv3.setText(o.email);     
     } 
     return v; 
    } 
} 

/* Phonebook class */ 
public class Phonebook{  
    public String name; 
    public String phnnum; 
    public String email; 
    public Phonebook(){ 
     super(); 
    } 

    public Phonebook(String name, String phnnum, String email) { 
     super(); 
     this.name = name; 
     this.phnnum = phnnum; 
     this.email = email; 
    } 
} 
관련 문제