編輯:關於Android編程
最近在做一個關於招聘的APP,裡面有選擇城市一項,是用listview分類,有的人說兩個listview嵌套,但是感覺太麻煩了,比較listview底層太復雜,有的人用expandablelistview,但是感覺效果一樣不好,所以寫了一個比較簡單的,希望對大家有幫助!
1.首先看MainActivity類!
其實跟普通的代碼一樣,只是多了一個MyBean類!
package com.zhaoqingyuan.zhaoqingyuan; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class MainActivity extends Activity { private ListView listView; private MyAdapter myAdapter; private List2.這個是Bean類,裡面有get和set方法!list=new ArrayList ();//裝數據的 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView) findViewById(R.id.listView1); //裝數據了 //當前位置 MyBean myBean=new MyBean("當前位置", "北京"); list.add(myBean); //熱門城市 String[] hotCity={"全國","北京","上海","武漢","廣州","天津","太原","南京","深圳","重慶","昆明","拉薩","石家莊","哈爾濱","貴陽"}; for (int i = 0; i < hotCity.length; i++) { MyBean myBean2=new MyBean("熱門城市", hotCity[i]); list.add(myBean2); } //熱門城市 String[] province={"河北省","河南省","雲南省","遼寧省","黑龍江省","湖南省","安徽省","山東省","江蘇省","浙江省", " 江西省","湖北省","甘肅省","山西省","內蒙古","陝西省","吉林省","福建省","貴州省","廣東省", "青海省","西藏","四川省","寧夏回族","海南省","台灣省"}; for (int i = 0; i < province.length; i++) { MyBean myBean3=new MyBean("按省份選擇城市", province[i]); list.add(myBean3); } myAdapter=new MyAdapter(MainActivity.this, list); listView.setAdapter(myAdapter); } }
package com.zhaoqingyuan.zhaoqingyuan; public class MyBean { private String series; private String city;//可以替代省份名稱 public MyBean(String series,String city) { setSeries(series); setCity(city); } public String getSeries() { return series; } public void setSeries(String series) { this.series = series; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }3.接下來就是我們在熟悉不過的適配器了!就是多了兩行代碼而已!
package com.zhaoqingyuan.zhaoqingyuan; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MyAdapter extends BaseAdapter { private Context context; private List4.來看下簡單的XML代碼list=new ArrayList (); public MyAdapter(Context context,List list) { this.context=context; this.list=list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } Holder holder; @Override public View getView(int position, View convertView, ViewGroup parent) { holder=new Holder(); if (convertView==null) { convertView=LayoutInflater.from(context).inflate(R.layout.item, null); holder.tv_series=(TextView) convertView.findViewById(R.id.tv_series); holder.tv_city=(TextView) convertView.findViewById(R.id.tv_city); convertView.setTag(holder); }else { holder=(Holder) convertView.getTag(); } String series1=list.get(position).getSeries(); String series2=position-1>=0?list.get(position-1).getSeries():""; if (!series1.equals(series2)) { holder.tv_series.setVisibility(View.VISIBLE); System.out.println(position); holder.tv_series.setText(series1); holder.tv_city.setText(list.get(position).getCity()); }else { holder.tv_series.setVisibility(View.GONE); holder.tv_city.setText(list.get(position).getCity()); } return convertView; } private class Holder{ TextView tv_series,tv_city; } }
OK了,大功告成,讓我們看一下效果吧!
源碼點擊此處下載
Android OkHttp(一)初識,這篇文章最後提供了一個封裝Okhttp請求的類,今天就來看看在項目中具體的使用情況。一、簡單接口請求。接口請求,需要有一個服務端,
這篇文章主要介紹在實際Android應用程序的開發中,容易導致內存洩露的一些情況。開發人員如果在進行代碼編寫之前就有內存洩露方面的基礎知 識,那麼
一、更新軟件的准備 在線更新軟件的話需要我們有簽名的應用,我們需要把簽過名之後的軟件放入到服務器中,我的如下: 其中apk是有簽名的更新版本! updateinfo.h
RatingBar控件為用戶提供一個評分操作的模式。 控件的幾個常用方法:1.setMax()設置RatingBar星級滑塊的最大值.2.setNumStars