Github地址:https://github.com/lvlife/gangedCitys
特别说明 省市的json数据存放在assets目录下 读取本地数据
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String ENCODING = "UTF-8";
private static final String TAG = "MainActivity";
private Button mButton;
private Area mArea;
private ArrayList mDatas;
private ListView mListView, mListView1;
private MainAdapter mMainAdapter, mMainAdapter2;
private ArrayList provinceName;
private ArrayList citysName;
private PopupWindow mCityPopupWindow;
private String aa, bb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
provinceName = new ArrayList<>();
mDatas = new ArrayList<>();
citysName = new ArrayList<>();
mButton = this.findViewById(R.id.button);
initPopWindowForCitys();
mButton.setOnClickListener(this);
}
private void initPopWindowForCitys() {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View layout = inflater.inflate(R.layout.wheelpopupforcity, null, false);
mListView = layout.findViewById(R.id.list);
mListView1 = layout.findViewById(R.id.list1);
layout.getBackground().setAlpha(130);
//更新UI
layout.invalidate();
mCityPopupWindow = new PopupWindow(layout, LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
Button btn_city_canle = layout.findViewById(R.id.pickcitycancle);
Button pickcityconfirm = layout.findViewById(R.id.pickcityconfirm);
btn_city_canle.getPaint().setFakeBoldText(true);
btn_city_canle.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCityPopupWindow.dismiss();
mCityPopupWindow.setFocusable(false);
}
});
pickcityconfirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String cc = aa + bb;
mCityPopupWindow.dismiss();
mCityPopupWindow.setFocusable(false);
Toast.makeText(MainActivity.this, cc, Toast.LENGTH_LONG).show();
}
});
String str1 = getFromAssets("leibie.json");
JSONObject js;
try {
js = new JSONObject(str1);
String areaBeans = js.getString("areaBeans");
JSONArray areaBean = new JSONArray(areaBeans);
for (int i = 0; i < areaBean.length(); i++) {
mArea = new Area();
JSONObject json = (JSONObject) areaBean.get(i);
mArea.setAreaid(json.optString("areaid"));
mArea.setName(json.optString("name"));
mArea.setPinyin(json.optString("pinyin"));
mArea.setShortpinyin(json.optString("shortpinyin"));
mArea.setType(json.optString("type"));
mArea.setParentId(json.optString("parentId"));
mDatas.add(i, mArea);
}
for (int i = 0; i < mDatas.size(); i++) {
if (mDatas.get(i).getType().equals("s")) {
provinceName.add(mDatas.get(i));
}
}
Log.d(TAG, "firstName:" + provinceName.get(0).toString());
mCityPopupWindow.setFocusable(true);
mMainAdapter = new MainAdapter(MainActivity.this, provinceName);
mMainAdapter2 = new MainAdapter(MainActivity.this, citysName);
mListView.setAdapter(mMainAdapter);
mListView1.setAdapter(mMainAdapter2);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3)
{
citysName.clear();
for (int i = 0; i < mDatas.size(); i++) {
String id = provinceName.get(arg2).getAreaid();
aa = provinceName.get(arg2).getName();
if (mDatas.get(i).getType().equals("c")) {
if (mDatas.get(i).getParentId().equals(id)) {
citysName.add(mDatas.get(i));
}
}
}
mMainAdapter2.notifyDataSetChanged();
}
});
mListView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3)
{
bb = citysName.get(arg2).getName();
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String getFromAssets(String fileName) {
String result = "";
try {
InputStream inputStream = getResources().getAssets().open(fileName);
// 获取文件的字节数
int length = inputStream.available();
// 创建byte数组
byte[] buffer = new byte[length];
// 将文件中的数据读到byte数组中
inputStream.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.button) {
mCityPopupWindow.setAnimationStyle(R.style.PopupAnimation);
mCityPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, 0, 0);
mCityPopupWindow.update();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
}
public class MainAdapter extends BaseAdapter {
private Context mContext;
private ArrayList mList;
private LayoutInflater mInflater;
private TextView mTextView;
public MainAdapter(Context context,ArrayListlist){
this.mContext =context;
this.mList =list;
this.mInflater =LayoutInflater.from(context);
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//判空
if (convertView==null) {
mTextView = new TextView(mContext);
}else {
mTextView = (TextView) convertView;
}
mTextView.setText(mList.get(position).getName());
mTextView.setTextSize(18);
mTextView.setGravity(Gravity.CENTER);
mTextView.setPadding(0,20,0,20);
mTextView.setTextColor(Color.BLACK);
return mTextView;
}
}
public class Area {
private String areaid;
private String name;
private String pinyin;
private String shortpinyin;
private String type;
private String parentId;
public String getAreaid() {
return areaid;
}
public void setAreaid(String areaid) {
this.areaid = areaid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPinyin() {
return pinyin;
}
public void setPinyin(String pinyin) {
this.pinyin = pinyin;
}
public String getShortpinyin() {
return shortpinyin;
}
public void setShortpinyin(String shortpinyin) {
this.shortpinyin = shortpinyin;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
}
android仿IOS的滚轮省市二级联动 实现还是比较简单的 就是2个ListView显示数据
A切换时通知B去做更新 !