Java Android-Custom Listview-JSONArray-ArrayAdapter始终选择最后一个

Java Android-Custom Listview-JSONArray-ArrayAdapter始终选择最后一个,java,android,Java,Android,首先,如果我的英语不好,我是德国人。 这就是我的问题: 我想做一个天气应用程序,现在我正在做天气预报部分。 我使用一个ArrayList,以保存所有天气临时的日子。 但在应用程序中,他总是使用ArrayList的最后一项,我找不到问题所在 这是我代码的一部分: LinearLayout linearLayout; private ArrayList<ForeCastWeather> list = new ArrayList<>(); CustomListAdap

首先,如果我的英语不好,我是德国人。 这就是我的问题: 我想做一个天气应用程序,现在我正在做天气预报部分。 我使用一个ArrayList,以保存所有天气临时的日子。 但在应用程序中,他总是使用ArrayList的最后一项,我找不到问题所在

这是我代码的一部分:

     LinearLayout linearLayout;
private ArrayList<ForeCastWeather> list = new ArrayList<>();
CustomListAdapter adapter;
ListView listView;
View rootView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.forecast_layout, container, false);

    return rootView;
}


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    linearLayout = (LinearLayout) rootView.findViewById(R.id.layout);

    listView = (ListView) rootView.findViewById(R.id.listView);
    adapter = new CustomListAdapter(getActivity(), list);
    listView.setAdapter(adapter);

    try {

        JSONObject json = ForecastWeatherData.getJsonObject();
        JSONArray jsonArray = json.getJSONArray("list");
        for (int i = 0; i < jsonArray.length(); i++) {

            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONObject temp = jsonObject.getJSONObject("temp");
            JSONArray weather = jsonObject.getJSONArray("weather");
            JSONObject weather_Object = weather.getJSONObject(0);

            ForeCastWeather foreCastWeather = new ForeCastWeather();

            foreCastWeather.setDt(jsonObject.getLong("dt"));

            foreCastWeather.setDay(temp.getDouble("day"));
            foreCastWeather.setMin(temp.getDouble("min"));
            foreCastWeather.setMax(temp.getDouble("max"));
            foreCastWeather.setNight(temp.getDouble("night"));
            foreCastWeather.setEve(temp.getDouble("eve"));
            foreCastWeather.setMorn(temp.getDouble("morn"));

            foreCastWeather.setPressure(jsonObject.getDouble("pressure"));
            foreCastWeather.setHumidity(jsonObject.getInt("humidity"));

            foreCastWeather.setID(weather_Object.getInt("id"));
            foreCastWeather.setMain(weather_Object.getString("main"));
            foreCastWeather.setDescription(weather_Object.getString("description"));
            foreCastWeather.setIcon(weather_Object.getString("icon"));

            foreCastWeather.setSpeed(jsonObject.getDouble("speed"));
            foreCastWeather.setDeg(jsonObject.getInt("deg"));
            foreCastWeather.setClouds(jsonObject.getInt("clouds"));
            try {
                foreCastWeather.setRain(jsonObject.getDouble("rain"));
            }
            catch (Exception e)
            {
                foreCastWeather.setRain(0.0);
            }
            list.add(foreCastWeather);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    adapter.setData(list);
    adapter.notifyDataSetChanged();


}
LinearLayout LinearLayout;
private ArrayList list=new ArrayList();
自定义列表适配器;
列表视图列表视图;
视图根视图;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
rootView=充气机。充气(R.layout.forecast\u布局,容器,false);
返回rootView;
}
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
linearLayout=(linearLayout)rootView.findviewbyd(R.id.layout);
listView=(listView)rootView.findViewById(R.id.listView);
adapter=新的CustomListAdapter(getActivity(),list);
setAdapter(适配器);
试一试{
JSONObject json=ForecastWeatherData.getJsonObject();
JSONArray JSONArray=json.getJSONArray(“列表”);
for(int i=0;i
这是我的适配器:

 private Activity activity;
private LayoutInflater layoutInflater;
private List<ForeCastWeather> foreCastWeathers;

public CustomListAdapter(Activity activity, List<ForeCastWeather> foreCastWeathers) {
    this.activity = activity;
    this.foreCastWeathers = foreCastWeathers;
}

@Override
public int getCount() {
    return foreCastWeathers.size();
}

@Override
public Object getItem(int position) {
    return foreCastWeathers.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (layoutInflater == null)
        layoutInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = layoutInflater.inflate(R.layout.list_item, null);

    TextView time = (TextView) convertView.findViewById(R.id.time);
    TextView temp = (TextView) convertView.findViewById(R.id.temp);
    TextView temp_max = (TextView) convertView.findViewById(R.id.temp_min);
    TextView temp_min = (TextView) convertView.findViewById(R.id.temp_max);
    TextView rain = (TextView) convertView.findViewById(R.id.rain);
    TextView strange = (TextView) convertView.findViewById(R.id.wind);
    TextView degree = (TextView) convertView.findViewById(R.id.deg);
    TextView clouds = (TextView) convertView.findViewById(R.id.cloud);
    TextView humidity = (TextView) convertView.findViewById(R.id.humid);
    TextView pressure = (TextView) convertView.findViewById(R.id.press);
    ForeCastWeather foreCastWeather = foreCastWeathers.get(position);

    try {
        time.setText(foreCastWeather.getDt() + "");
        temp.setText(foreCastWeather.getDay() + "");
        temp_max.setText(foreCastWeather.getMax() + "");
        temp_min.setText(foreCastWeather.getMin() + "");
        rain.setText(foreCastWeather.getRain() + "");
        strange.setText(foreCastWeather.getSpeed() + "");
        degree.setText(foreCastWeather.getDeg() + "");
        clouds.setText(foreCastWeather.getClouds() + "");
        humidity.setText(foreCastWeather.getHumidity() + "");
        pressure.setText(foreCastWeather.getPressure() + "");

    }
    catch (Exception e)
    {


    }

    return convertView;
}
私人活动;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
预报员名单;
公共CustomListAdapter(活动,列表预测天气){
这个。活动=活动;
this.foreCastWeathers=foreCastWeathers;
}
@凌驾
public int getCount(){
返回艏楼Weathers.size();
}
@凌驾
公共对象getItem(int位置){
返回艏楼天气。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(layoutInflater==null)
layoutInflater=(layoutInflater)活动
.getSystemService(上下文布局\充气机\服务);
if(convertView==null)
convertView=LayoutFlater.充气(R.layout.list_项,空);
TextView时间=(TextView)convertView.findViewById(R.id.time);
TextView temp=(TextView)convertView.findViewById(R.id.temp);
TextView temp_max=(TextView)convertView.findViewById(R.id.temp_min);
TextView temp_min=(TextView)convertView.findViewById(R.id.temp_max);
TextView rain=(TextView)convertView.findViewById(R.id.rain);
TextView奇怪=(TextView)convertView.findViewById(R.id.wind);
TextView度=(TextView)convertView.findViewById(R.id.deg);
TextView clouds=(TextView)convertView.findViewById(R.id.cloud);
TextView湿度=(TextView)convertView.findViewById(R.id.潮湿);
TextView压力=(TextView)convertView.findViewById(R.id.press);
ForeCastWeather=foreCastWeathers.get(位置);
试一试{
time.setText(foreCastWeather.getDt()+);
temp.setText(foreCastWeather.getDay()+);
temp_max.setText(foreCastWeather.getMax()+);
temp_min.setText(foreCastWeather.getMin()+);
rain.setText(foreCastWeather.getRain()+);
奇怪的.setText(foreCastWeather.getSpeed()+);
degree.setText(foreCastWeather.getDeg()+);
clouds.setText(foreCastWeather.getClouds()+);
湿度.setText(foreCastWeather.get湿度()+);
pressure.setText(foreCastWeather.getPressure()+);
}
捕获(例外e)
{
}
返回视图;
}
}

谢谢你的帮助


Jonah

假设此代码存在于您的
活动中,我认为您缺少的是将新数据分配给适配器。您正在使用
notifyDataSetChanged()
通知适配器数据已更改,但适配器正在使用的数据可能尚未更改

在适配器的
getView()
方法中,用于表示列表的数据来自哪里?如果它是适配器类中的一个全局变量,那么您必须创建某种方法来将适配器的列表设置为新的
public void setData(List<ForeCastWeather> newList) {
    foreCastWeathers = newList;
}
adapter.setData(list);
adapter.notifyDataSetChanged()
    try {
        JSONObject json = ForecastWeatherData.getJsonObject();
        JSONArray jsonArray = json.getJSONArray("list");

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONObject temp = jsonObject.getJSONObject("temp");
            JSONArray weather = jsonObject.getJSONArray("weather");
            JSONObject weather_Object = weather.getJSONObject(0);

            ForeCastWeather foreCastWeather = new ForeCastWeather();

            foreCastWeather.setDt(jsonObject.getLong("dt"));

            foreCastWeather.setDay(temp.getDouble("day"));
            foreCastWeather.setMin(temp.getDouble("min"));
            foreCastWeather.setMax(temp.getDouble("max"));
            foreCastWeather.setNight(temp.getDouble("night"));
            foreCastWeather.setEve(temp.getDouble("eve"));
            foreCastWeather.setMorn(temp.getDouble("morn"));
            foreCastWeather.setPressure(jsonObject.getDouble("pressure"));
            foreCastWeather.setHumidity(jsonObject.getInt("humidity"));
            foreCastWeather.setID(weather_Object.getInt("id"));
            foreCastWeather.setMain(weather_Object.getString("main"));
            foreCastWeather.setDescription(weather_Object.getString("description"));
            foreCastWeather.setIcon(weather_Object.getString("icon"));
            foreCastWeather.setSpeed(jsonObject.getDouble("speed"));
            foreCastWeather.setDeg(jsonObject.getInt("deg"));
            foreCastWeather.setClouds(jsonObject.getInt("clouds"));

            try {
                foreCastWeather.setRain(jsonObject.getDouble("rain"));
            } catch (Exception e) {
                foreCastWeather.setRain(0.0);
            }

            list.add(foreCastWeather); // we're adding the new object to this list, not the list in the adapter.
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    adapter.setData(list); // this is the important addition
    adapter.notifyDataSetChanged();
}
    private Activity activity;
    private LayoutInflater layoutInflater;
    private List<ForeCastWeather> foreCastWeathers;

    public CustomListAdapter(Activity activity, List<ForeCastWeather> foreCastWeathers) {
        this.activity = activity;
        this.foreCastWeathers = foreCastWeathers;
    }

    /**
     * The addition is here
     */
    public void setData(List<ForeCastWeather> newList) {
        this.foreCastWeathers = newList;
    }

    @Override
    public int getCount() {
        return foreCastWeathers.size();
    }

    @Override
    public Object getItem(int position) {
        return foreCastWeathers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (layoutInflater == null)
            layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null)
            convertView = layoutInflater.inflate(R.layout.list_item, null);

        TextView time = (TextView) convertView.findViewById(R.id.time);
        TextView temp = (TextView) convertView.findViewById(R.id.temp);
        TextView temp_max = (TextView) convertView.findViewById(R.id.temp_min);
        TextView temp_min = (TextView) convertView.findViewById(R.id.temp_max);
        TextView rain = (TextView) convertView.findViewById(R.id.rain);
        TextView strange = (TextView) convertView.findViewById(R.id.wind);
        TextView degree = (TextView) convertView.findViewById(R.id.deg);
        TextView clouds = (TextView) convertView.findViewById(R.id.cloud);
        TextView humidity = (TextView) convertView.findViewById(R.id.humid);
        TextView pressure = (TextView) convertView.findViewById(R.id.press);
        ForeCastWeather foreCastWeather = foreCastWeathers.get(position);

        try { // you shouldn't really need this try catch.
            time.setText(foreCastWeather.getDt() + "");
            temp.setText(foreCastWeather.getDay() + "");
            temp_max.setText(foreCastWeather.getMax() + "");
            temp_min.setText(foreCastWeather.getMin() + "");
            rain.setText(foreCastWeather.getRain() + "");
            strange.setText(foreCastWeather.getSpeed() + "");
            degree.setText(foreCastWeather.getDeg() + "");
            clouds.setText(foreCastWeather.getClouds() + "");
            humidity.setText(foreCastWeather.getHumidity() + "");
            pressure.setText(foreCastWeather.getPressure() + "");
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }
}