我总是遇到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));
}
}
答案 0 :(得分:0)
每当你的应用程序崩溃时,你应该在你的问题中发布错误,否则我们必须猜测......这些是我看到的问题。 (可能会有更多。)
list
不是类变量,因此setupList()
无法看到它。 items
String
分配给NameValuePair
对象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));
}
}
答案 1 :(得分:0)
看起来您正在尝试将String添加到NameValuePair变量列表中。
你需要转换你的“la ville:”+ fd_name +“la tempurature”+ fd_temp +“le climat”+ fd_desc 字符串到NameValuePair变量。
在不知道如何实施的情况下,我无法提供如何实施的建议。