Android简单版天气预报,显示天气预报(第二步)
接着上一步的:[https://blog.csdn.net/weixin_44889138/article/details/102748057]
上一步的链接
ListView的改进,因为每次在getView()方法中还是会调用View的findViewById()方法来获取一次控件的实例,写一个内部类,利用**view.setTag()**存储,getTag()取。其实实现的机制跟RecycleView差不多
package com.example.xhhweather;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class CityAdapter extends BaseAdapter {
//
class ViewHolder{
TextView TVcityName;
//删除城市
ImageView ivDelCity;
//选择城市后返回天气界面
ImageView ivQuyCity;
}
private List<CityItem> list;
public CityAdapter(List<CityItem> list){
this.list=list;
}
@Override
public View getView(final int position, View covertView, final ViewGroup viewGroup) {
View view;
ViewHolder viewHolder;
if(covertView==null){
view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.city_item,viewGroup,false);
viewHolder=new ViewHolder();
viewHolder.TVcityName=view.findViewById(R.id.tv_city_name);
viewHolder.ivQuyCity=view.findViewById(R.id.btn_move);
viewHolder.ivDelCity=view.findViewById(R.id.iv_del_city);
//将viewHolder存入在view中
view.setTag(viewHolder);
}else{
view=covertView;
viewHolder= (ViewHolder) view.getTag();
}
CityItem cityItem= list.get(position);
viewHolder.TVcityName.setText(cityItem.getCityName());
//删除城市
viewHolder.ivDelCity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.remove(position);
notifyDataSetChanged();//本身就是adapter
}
});
//移动城市
viewHolder.ivQuyCity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CityItem city=list.get(position);
Toast.makeText(viewGroup.getContext(),city.getCityPinYin(),Toast.LENGTH_SHORT).show();
}
});
return view;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
}
package com.example.xhhweather;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.example.xhhweather.gson.Weather;
import com.example.xhhweather.gson.WeatherResDaily;
import com.example.xhhweather.gson.WeatherResults;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity{
private Button btnSelectCity;
private TextView weatherTitleCityname;
private TextView tvFirstDayDate;
private TextView tvFirstDayPhe;
private TextView tvFirstDayHigh;
private TextView tvFirstDayLow;
private TextView tvSecondDayDate;
private TextView tvSecondDayPhe;
private TextView tvSecondDayHigh;
private TextView tvSecondDayLow;
private TextView tvThirdDayDate;
private TextView tvThirdDayPhe;
private TextView tvThirdDayHigh;
private TextView tvThirdDayLow;
private TextView temperatureText;
private TextView caseText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout