zoukankan      html  css  js  c++  java
  • android spinner异步加载数据并显示

    一开始直接绑定适配器发现点击后并不能显示选中的值。
    解决办法:利用handle来更新UI。
    我利用接口和okhttp的get方法从数据库拿到所有省份信息的json数据,在android中定义了一个province实体类来接收list。
    province类
    package com.example.demo.entity;
    
    public class Province {
        private String province;
    
        private Province(){}
    
        public String getProvince() {
            return province;
        }
    
        public Province(String province) {
            this.province = province;
        }
    
        public void setProvince(String province) {
            this.province = province;
        }
    }
    重写adapter类实现该方法
    package com.example.demo.entity;
    
    import android.content.Context;
    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 com.example.demo.R;
    
    import java.util.List;
    
    public class MapAdapter extends BaseAdapter {
        private Context context;
        private List<Province> mData;
    
        public MapAdapter(List<Province> mData, Context context) {
            this.mData = mData;
            this.context = context;
        }
        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }
    
        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_spin_map
                        , parent
                        , false);
                holder.name = (TextView) convertView.findViewById(R.id.map_name);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.name.setText(mData.get(position).getProvince());
            return convertView;
        }
    
        static class ViewHolder {
            private TextView name;
        }
    }
    View Code
    重点:在activity的onCreate方法中先使用okhttp的get方法获得spinner要显示的对象,此时,
    适配器的set语句(下面三句)不能与get方法一起放在onCreate方法中。因为数据是异步加载的。
                    mapAdapter = new MapAdapter(provinceData,context);
                    spin_Province.setAdapter(mapAdapter);
                    spin_Province.setOnItemSelectedListener(MapPageActivity.this);
    利用handle类来完成适配
    private Handler myHandler = new Handler() {
            @Override
            //重写handleMessage方法,根据msg中what的值判断是否执行后续操作
            public void handleMessage(Message msg) {
                if (msg.what == 0) {
                    mapAdapter = new MapAdapter(provinceData,context);
                    spin_Province.setAdapter(mapAdapter);
                    spin_Province.setOnItemSelectedListener(MapPageActivity.this);
                }
            }
        };
    启动这个类的语句放在上面get方法的末尾,确保数据全部拿到了,再适配
    myHandler.sendEmptyMessage(0);
    总体activity类如下:
    package com.example.demo.activity;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Looper;
    import android.os.Message;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.Spinner;
    import android.widget.Toast;
    
    import com.example.demo.ActivityCollector;
    import com.example.demo.R;
    import com.example.demo.entity.Province;
    import com.example.demo.entity.MapAdapter;
    import com.google.gson.Gson;
    import com.google.gson.reflect.TypeToken;
    
    import org.json.JSONArray;
    
    import java.io.IOException;
    import java.lang.reflect.Type;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import okhttp3.Call;
    import okhttp3.Callback;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    
    public class MapPageActivity extends Activity implements AdapterView.OnItemSelectedListener{
        private Button btnCity,btnCounty;
        private Spinner spin_Province;
        private Context context;
        private List<Province> provinceData;
        private MapAdapter mapAdapter = null;
        //判断是否为刚进去时触发onItemSelected的标志
        private boolean one_selected = false;
        private boolean two_selected = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            context = this;
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map_page);
            provinceData = new ArrayList<>();
            ActivityCollector.addActivity(this);
            spin_Province = (Spinner)findViewById(R.id.spin_province);
            btnCity = (Button)findViewById(R.id.btncity);
            btnCounty = (Button)findViewById(R.id.btncounty);
            getProvince();
    
        }
    
        private void init(){
    
        }
    
        private void getProvince() {
            //第一步获取okHttpClient对象
            OkHttpClient client = new OkHttpClient.Builder()
                    .build();
            //第二步构建Request对象
            String url = "http://192.168.236.1:8181/sewagePlant/province";
            Request request = new Request.Builder()
                    .url(url)
                    .get()
                    .build();
            //第三步构建Call对象
            Call call = client.newCall(request);
            //第四步:异步get请求
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.i("ttit", e.getMessage());
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    //得到的子线程
                    String result = response.body().string();
                    Gson gson = new Gson();
    //                Type type = new TypeToken<List<Province>>(){}.getType();
                    List<Province> provinces = gson.fromJson(result, new TypeToken<List<Province>>() {}.getType());
                    for (int i = 0; i <provinces.size() ; i++) {
                        Province p = provinces.get(i);
                        provinceData.add(p);
                        Log.i("ttit",p.getProvince());
                    }
                    myHandler.sendEmptyMessage(0);
                }
            });
        }
    
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
            switch (parent.getId()){
                case R.id.spin_province:
                    if (one_selected){
    
                        Toast.makeText(MapPageActivity.this,"选择了"+parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
                    }else {
                        one_selected = true;
                    }
                    break;
            }
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
    
        }
    
        private Handler myHandler = new Handler() {
            @Override
            //重写handleMessage方法,根据msg中what的值判断是否执行后续操作
            public void handleMessage(Message msg) {
                if (msg.what == 0) {
                    mapAdapter = new MapAdapter(provinceData,context);
                    spin_Province.setAdapter(mapAdapter);
                    spin_Province.setOnItemSelectedListener(MapPageActivity.this);
                }
            }
        };
    }
    View Code
     
  • 相关阅读:
    实现UILabel渐变色效果
    设计模式-原型模式
    计算一/二元一次方程的类(用于动画控制)
    【转】VMware网络连接模式—桥接、NAT以及仅主机模式的详细介绍和区别
    【转】VMware虚拟机系统无法上网怎么办?
    【转】Android tools:context
    【转】android布局--Android fill_parent、wrap_content和match_parent的区别
    【转】在程序中设置android:gravity 和 android:layout_Gravity属性
    【转】android gravity属性 和 weight属性
    【转】Android xml资源文件中@、@android:type、@*、?、@+含义和区别
  • 原文地址:https://www.cnblogs.com/Meng2113/p/13667354.html
Copyright © 2011-2022 走看看