私は、mysqlからのデータを(名前でリストされた)オフィスのリストビューとして表示したいと思います。リストアイテムをクリックすると、その事務所今、リストは表示されません。私はAndroidの新機能です(私は2週間前に起動します)ので、修正する方法を理解できません...誰かが私を助けてくれますか?あなたはsetContentView(...)
をコメントしている場合は、(ListView)findViewById(R.id.officeL)
を使用することはできませんListActivity
...アンドロイドトラフのListビューとしてのMysqlクエリの結果JSON
//setContentView(R.layout.office);
officeList = (ListView)findViewById(R.id.officeL);
でこれを行うことはできません
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);
}
}
ありがとうございました! OKリストは修正されました...しかし、とにかく動作しません...問題は、JSON変換であるようです...任意のヒントについては? – user1099798
ロギングを使用して 'result'と' result1'の配列要素が何であるかを確認します。それが私がそれをする唯一の方法です。 – Squonk