-3

我总是对 item.add 和方法设置列表有问题

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
  • 您正在尝试将 a 分配StringNameValuePair对象
  • AString与a有很大不同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 回答