安卓问题,代码如下:想显示一个listView。但是跳转到合格activity什么都不显示。

package com.coolweather.app.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.coolweather.app.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebIconDatabase.IconListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Future extends Activity{

private ListView lvFur;
private static List list= new ArrayList();
private TextView tvTest;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.furture);
lvFur = (ListView) findViewById(R.id.listView1);
WeatherActivity wa = new WeatherActivity();
handleWeatherResponse(wa.result);
lvFur.setAdapter(adapter);

}
public BaseAdapter adapter = new BaseAdapter() {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(Future.this);
        if(convertView==null){
            convertView = inflater.inflate(R.layout.item, null);
            TextView tv_data = (TextView) convertView.findViewById(R.id.textView_data);
            TextView tv_wendu = (TextView) convertView.findViewById(R.id.textView_wendu);
            ImageView iv_head = (ImageView)convertView.findViewById(R.id.imageView_head);
            tv_data.setText(list.get(position).getCityname()+list.get(position).getDate());
            tv_wendu.setText(list.get(position).getWeather()+list.get(position).getTmper());
            ViewHolder vh= new ViewHolder(tv_data,tv_wendu);
            convertView.setTag(vh);
        }else{
            ViewHolder vh = (ViewHolder) convertView.getTag();

            vh.tv_data.setText(list.get(position).getCityname()+list.get(position).getDate());
            vh.tv_wendu.setText(list.get(position).getWeather()+list.get(position).getTmper());
        }
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }
};
public class ViewHolder{
    private TextView tv_data;
    private TextView tv_wendu;
    //private ImageView iv_head;
    public TextView getTv_data() {
        return tv_data;
    }
    public void setTv_data(TextView tv_data) {
        this.tv_data = tv_data;
    }
    public TextView getTv_wendu() {
        return tv_wendu;
    }
    public void setTv_wendu(TextView tv_wendu) {
        this.tv_wendu = tv_wendu;
    }

// public ImageView getIv_head() {
// return iv_head;
// }
// public void setIv_head(ImageView iv_head) {
// this.iv_head = iv_head;
// }

public ViewHolder(TextView tv_data, TextView tv_wendu) {
super();
this.tv_data = tv_data;
this.tv_wendu = tv_wendu;
//this.iv_head = iv_head;
}

}
//解析joson数据
public static void handleWeatherResponse(String response) {
    try {
        JSONArray root = new JSONArray(response);
        for(int i = 0;i < root.length();i++){
            JSONObject item = root.getJSONObject(i);
            String date = item.getString("days");
            String week = item.getString("week");
            String weather = item.getString("weather");
            String cityname = item.getString("citynm");
            String tmp = item.getString("temperature");
            list.add(new Furweather(date+week,weather,cityname, tmp));
        }
    }catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}
class Furweather{
private String date;
private String weather;
private String cityname;
private String tmper;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public String getCityname() {
return cityname;
}
public void setCityname(String cityname) {
this.cityname = cityname;
}
public String getTmper() {
return tmper;
}
public void setTmper(String tmper) {
this.tmper = tmper;
}
public Furweather(String date, String weather, String cityname,
String tmper) {
super();
this.date = date;
this.weather = weather;
this.cityname = cityname;
this.tmper = tmper;
}

}

其中result是从另一个activity中传过来的joson数据。现在打开这个activity没有显示。求大神指出是哪里的问题。。

4个回答

这是你写的:
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

改成:

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

你的很多写法跟我的惯性写法都不太一样,先这样改了试试看

tc199604
tc199604 我的list里好像就是没有数据。。。请问我的josn解析是对的吗??
2 年多之前 回复
qq_33756493
昵称_Justin 回复tc199604: 你断点看看吧,很简单的,或者你在add哪里打印List数据,如果有数据,你可以按照楼上的方法,正确是:public int getCount() { // TODO Auto-generated method stub return data.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return data.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; }
2 年多之前 回复
tc199604
tc199604 这一句不是new了新对象添加到list、里了么??
2 年多之前 回复
tc199604
tc199604 list.add(new Furweather(date+week,weather,cityname, tmp));
2 年多之前 回复
qq_33756493
昵称_Justin 回复tc199604: 你的list里面没有数据啊,你要把你解析得到的数据添加到List里面的
2 年多之前 回复
tc199604
tc199604 似乎是我的list里并没有写入数据。。。不知道是为什么。。
2 年多之前 回复

数据都没有哪里显示的出来

tc199604
tc199604 我的数据是从 其他的activity传过来的 能加你的qq么??
2 年多之前 回复

图片说明这是我返回的josn数据

没数据显示的话,你打印下你输出的数据到底获取到没有,然后再去看看是不是适配器错误,多打几个log

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐