2011-12-18 3 views
1

mysql의 데이터를 이름으로 나열된 사무실 목록보기로 표시하고 목록 항목을 클릭하면 자세한 정보를 얻을 수 있습니다. 그 사무실. 이제 목록이 나타나지 않습니다. 나는 안드로이드 (2 주 전부터 시작)에 새로운 사람입니다. 그래서 어떻게 고쳐야하는지 이해할 수 없습니다. 제발 저를 도울 수 있습니까?android trough에서 목록보기로 MySQL 쿼리 결과 JSON

public class Office extends ListActivity implements OnItemClickListener{ 


ListView officeList; 
InputStream is = null; 

String result = ""; 
String result1 []; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

//setContentView(R.layout.office); 
officeList = (ListView)findViewById(R.id.officeL); 



//the year data to send 
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("id","0")); 


//http post 
try{ 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://www.protocolliweb.com/mysqlcon.php"); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity(); 
    is = entity.getContent(); 

}catch(Exception e){ 
     Log.e("log_tag", "Error in http connection "+e.toString()); 
} 

//convert response to string 
try{ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
     } 
     is.close(); 
     result=sb.toString(); 

     JSONArray jArray = new JSONArray(result); 

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

        result1[i] = json_data.getString("nome"); 

        } 

      officeList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, result1)); 
      officeList.setOnItemClickListener(this); 

}catch(Exception e){ 
     Log.e("log_tag", "Error converting result "+e.toString()); 
} 

}

public void onItemClick(AdapterView<?> parent, View view, 
     int position, long id) { 

    String bread = officeList.getContext().toString(); 
    Bundle basket = new Bundle(); 
    basket.putString("key", bread); 
    Intent a = new Intent(Office.this,Details.class); 
    a.putExtras(basket); 
    startActivity(a); 
    } 

}

당신은 ListActivity에서이 작업을 수행 할 수

답변

1

... 당신이 주석 setContentView(...)이있는 경우

//setContentView(R.layout.office); 
officeList = (ListView)findViewById(R.id.officeL); 

당신은 (ListView)findViewById(R.id.officeL)을 사용할 수 없습니다 아웃. 사용자 정의 레이아웃을 사용하여 다음과 같은 일을하려면

당신이 콘텐츠보기를 설정하지 않은 경우

, ListActivity는 자신의 기본 사용 액세스 ListView ...

officeList = getListView(); 

를 사용합니다 ..

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp"> 

    <ListView android:id="@android:id/list" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#00FF00" 
       android:layout_weight="1" 
       android:drawSelectorOnTop="false"/> 

    <TextView android:id="@android:id/empty" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#FF0000" 
       android:text="No data"/> 
</LinearLayout> 

... 및 setContentView(<name_of_custom_xml_layout>)을 사용하십시오. 를 검색하고 getListView()에 의해 반환 된 ListViewidListActivity한다는 것이다 ListViewidandroid:id="@android:id/list"입니다

참고.

사용 방법을 이해하려면 ListActivity 문서를 참조하십시오.

+0

고마워요! 확인 목록 수정됩니다 ...하지만 어쨌든 작동하지 않습니다 ... 문제는 JSON coversion ... 어떤 팁에 대한 것? – user1099798

+0

로깅을 사용하여'result'와'result1'의 배열 요소를 봅니다. 그것이 내가 할 수있는 유일한 방법이다. – Squonk