自定义PopupWindow实现3急地区联动
2020-12-13 02:34
标签:popupwindow 3级联动 做项目时有时我们会需要3级联动,比如注册,买东西下单等,这里我在android上使用popupwindow实现3级联动功能,我实现的思路是,当程序启动时就将后台的地区JSON数据格式全部加载上来,通过SharedPreferences将获取到的数据保存,点击按钮获取SharedPreferences中的地区数据,再通过JSONObject转为List集合,具体实现如下: 布局文件: activity_main.xml: RegionsAdapter类
自定义PopupWindow实现3急地区联动,搜素材,soscw.com 自定义PopupWindow实现3急地区联动 标签:popupwindow 3级联动 原文地址:http://blog.csdn.net/tenghu8888/article/details/28888147
regions_popup_window.xml——>这个布局文件是PopupWindow使用
group_item.xml——>ListView使用
地区实体类——>根据后台返回JSON数据字段封装
package com.tenghu.model;
import java.io.Serializable;
import java.util.List;
public class Regions implements Serializable{
private int region_id;
private Integer p_region_id;
private String region_path;
private int region_grade;
private String local_name;
private List
省略get与set方法
package com.tenghu.adapter;
import java.util.List;
import com.example.popupwindow.R;
import com.tenghu.model.Regions;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class RegionsAdapter extends BaseAdapter{
private Context context;
private List
HttpUtil工具类
package com.tenghu.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class HttpUtil {
/**
* Get请求
* @param urls
* @return
*/
public static String getRequest(String urls){
try {
//创建HttpClient对象
HttpClient client=new DefaultHttpClient();
//创建Get请求
HttpGet get=new HttpGet(urls);
//获取请求响应
HttpResponse response=client.execute(get);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
//获取InputStream对象
InputStream is=response.getEntity().getContent();
//创建BufferedReader
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=null;
StringBuffer sb=new StringBuffer();
while((line=br.readLine())!=null){
sb.append(line);
}
return sb.toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
MainActivity类
package com.example.popupwindow;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.tenghu.adapter.RegionsAdapter;
import com.tenghu.model.Regions;
import com.tenghu.util.HttpUtil;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class MainActivity extends Activity {
private PopupWindow pw=null;//自定义PopupWindow提示框
private ListView regionsLv;//ListView空间
private List
后台返回数据格式,部分:
[{"region_id":1,"p_region_id":0,"region_path":",1,","region_grade":1,"local_name":"北京市","children":[{"region_id":2,"p_region_id":1,"region_path":",1,2,","region_grade":2,"local_name":"北京市","children":[{"region_id":3,"p_region_id":2,"region_path":",1,2,3,","region_grade":3,"local_name":"东城区","children":[]},{"region_id":4,"p_region_id":2,"region_path":",1,2,4,","region_grade":3,"local_name":"西城区","children":[]},{"region_id":5,"p_region_id":2,"region_path":",1,2,5,","region_grade":3,"local_name":"崇文区","children":[]},{"region_id":6,"p_region_id":2,"region_path":",1,2,6,","region_grade":3,"local_name":"宣武区","children":[]},{"region_id":7,"p_region_id":2,"region_path":",1,2,7,","region_grade":3,"local_name":"朝阳区","children":[]},{"region_id":8,"p_region_id":2,"region_path":",1,2,8,","region_grade":3,"local_name":"丰台区","children":[]},{"region_id":9,"p_region_id":2,"region_path":",1,2,9,","region_grade":3,"local_name":"石景山区","children":[]},{"region_id":10,"p_region_id":2,"region_path":",1,2,10,","region_grade":3,"local_name":"海淀区","children":[]},{"region_id":11,"p_region_id":2,"region_path":",1,2,11,","region_grade":3,"local_name":"门头沟区","children":[]},{"region_id":12,"p_region_id":2,"region_path":",1,2,12,","region_grade":3,"local_name":"房山区","children":[]},{"region_id":13,"p_region_id":2,"region_path":",1,2,13,","region_grade":3,"local_name":"通州区","children":[]},{"region_id":14,"p_region_id":2,"region_path":",1,2,14,","region_grade":3,"local_name":"顺义区","children":[]},{"region_id":15,"p_region_id":2,"region_path":",1,2,15,","region_grade":3,"local_name":"昌平区","children":[]},{"region_id":16,"p_region_id":2,"region_path":",1,2,16,","region_grade":3,"local_name":"大兴区","children":[]},{"region_id":17,"p_region_id":2,"region_path":",1,2,17,","region_grade":3,"local_name":"怀柔区","children":[]},{"region_id":18,"p_region_id":2,"region_path":",1,2,18,","region_grade":3,"local_name":"平谷区","children":[]},{"region_id":19,"p_region_id":2,"region_path":",1,2,19,","region_grade":3,"local_name":"密云县","children":[]},{"region_id":20,"p_region_id":2,"region_path":",1,2,20,","region_grade":3,"local_name":"延庆县","children":[]}]}]},
整个android代码都在这里了