-3

私はいつも item.add と methode setuplist に問題があります

public class WeatherMainActivity extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weather_main);

        ListView List = (ListView)findViewById(R.id.listView1);
        InputStream is = null;
        String result="";
        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.1.4/www/base_de_donnee_stage/script.php");
            List<NameValuePair> nameValue= new ArrayList<NameValuePair>();
            httppost.setEntity(new UrlEncodedFormEntity(nameValue));

            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();
            sb.append(reader.readLine() + "\n");
            String line="0";

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

            is.close();
            result=sb.toString();

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

        //paring data
        int fd_temp;
        String fd_name;
        String fd_desc;
        try{
            JSONArray jArray = new JSONArray(result);
            JSONObject json_data=null;

        for(int i=0;i<jArray.length();i++){
                json_data = jArray.getJSONObject(i);
                fd_name=json_data.getString("nom_ville");
                fd_temp=json_data.getInt("temperature");
                fd_desc=json_data.getString("description");
                List<NameValuePair> items;
                items.add("la ville:"+fd_name+"la tempurature"+fd_temp+"le climat"+fd_desc);
        }

        }
        catch(JSONException e1){
            Toast.makeText(getBaseContext(), ",hdsjh", Toast.LENGTH_LONG).show();
        }catch (ParseException e1){
            e1.printStackTrace();
        }
    }
private void setuplist(){
        list.setAdapter(new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,items));
    }
}
4

2 に答える 2

0

アプリがクラッシュするたびに、質問にエラーを投稿する必要があります。そうしないと、推測する必要があります...これらは私が見た問題です。(もっとあるかもしれません。)

  • listはクラス変数ではないため、setupList()表示できません。
  • 初期化することはありませんitems
  • オブジェクトにを割り当てようとしStringていますNameValuePair
  • AはとStringは大きく異なりますstring

これに一致するようにクラスを変更します。

public class WeatherMainActivity extends ListActivity {
    // Declare list out here
    ListView list;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weather_main);

        list = (ListView)findViewById(R.id.listView1);
        // Declare and initialize items here
        List<String> items = new ArrayList<String>();
        ...
    }

    private void setuplist(){
        // String is not string, notice the change I made
        list.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items));
    }
}
于 2012-09-17T18:03:10.407 に答える
0

NameValuePair 変数のリストに文字列を追加しようとしているようです。

「la ville:」+fd_name+「la tempurature」+fd_temp+「le climat」+fd_desc 文字列を NameValuePair 変数に変換する必要があります。

それがどのように実装されているかを知らなければ、その方法を提案することはできません。

于 2012-09-17T18:04:37.917 に答える