最近在工作之余,挤了点时间写这篇博客,是公司项目要实现一个功能,就是:选择国家和地区还有对应的编号,类似于下拉选择联系人的列表一样的,上张图:
package com.city.list.main;
import java.io.File;
import java.util.ArrayList;
import com.city.list.db.DBManager;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
Button btn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.selectBtn);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
startActivity(new Intent(MainActivity.this, CityList.class));
}
});
}
}
package com.city.list.main;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.city.list.db.DBManager;
import com.city.list.main.MyLetterListView.OnTouchingLetterChangedListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 城市列表
*
* @author sy
*
*/
public class CityList extends Activity
{
private BaseAdapter adapter;
private ListView mCityLit;
private TextView overlay;
private MyLetterListView letterListView;
private HashMap alphaIndexer;// 存放存在的汉语拼音首字母和与之对应的列表位置
private String[] sections;// 存放存在的汉语拼音首字母
private Handler handler;
private OverlayThread overlayThread;
private SQLiteDatabase database;
private ArrayList mCityNames;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.city_list);
mCityLit = (ListView) findViewById(R.id.city_list);
letterListView = (MyLetterListView) findViewById(R.id.cityLetterListView);
initHeaderView();
DBManager dbManager = new DBManager(this);
dbManager.openDateBase();
dbManager.closeDatabase();
database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
mCityNames = getCityNames();
database.close();
letterListView.setOnTouchingLetterChangedListener(new LetterListViewListener());
alphaIndexer = new HashMap();
handler = new Handler();
overlayThread = new OverlayThread();
initOverlay();
setAdapter(mCityNames);
mCityLit.setOnItemClickListener(new CityListOnItemClick());
}
private void initHeaderView() {
AbsListView.LayoutParams localLayoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
View headerView = View.inflate(this, R.layout.header_view, null);
headerView.setLayoutParams(localLayoutParams);
mCityLit.addHeaderView(headerView);
}
/**
* 从数据库获取城市数据
*
* @return
*/
private ArrayList getCityNames()
{
ArrayList names = new ArrayList();
Cursor cursor = database.rawQuery("SELECT * FROM T_City ORDER BY NameSort", null);
for (int i = 0; i < cursor.getCount(); i++)
{
cursor.moveToPosition(i);
CityModel cityModel = new CityModel();
cityModel.setCityName(cursor.getString(cursor.getColumnIndex("CityName")));
cityModel.setNameSort(cursor.getString(cursor.getColumnIndex("NameSort")));
cityModel.setAreaCode(cursor.getString(cursor.getColumnIndex("AreaCode")));
names.add(cityModel);
}
return names;
}
/**
* 城市列表点击事件
*
* @author sy
*
*/
class CityListOnItemClick implements OnItemClickListener
{
@Override
public void onItemClick(AdapterView> arg0, View arg1, int pos, long arg3)
{
if(pos!=0){
CityModel cityModel = (CityModel) mCityLit.getAdapter().getItem(pos);
Toast.makeText(CityList.this, cityModel.getCityName(), Toast.LENGTH_SHORT).show();
}
}
}
/**
* 为ListView设置适配器
*
* @param list
*/
private void setAdapter(List list)
{
if (list != null)
{
adapter = new ListAdapter(this, list);
mCityLit.setAdapter(adapter);
}
}
/**
* ListViewAdapter
*
* @author sy
*
*/
private class ListAdapter extends BaseAdapter
{
private LayoutInflater inflater;
private List list;
public ListAdapter(Context context, List list)
{
this.inflater = LayoutInflater.from(context);
this.list = list;
alphaIndexer = new HashMap();
sections = new String[list.size()];
for (int i = 0; i < list.size(); i++)
{
// 当前汉语拼音首字母
// getAlpha(list.get(i));
String currentStr = list.get(i).getNameSort();
// 上一个汉语拼音首字母,如果不存在为“ ”
String previewStr = (i - 1) >= 0 ? list.get(i - 1).getNameSort() : " ";
if (!previewStr.equals(currentStr))
{
String name = list.get(i).getNameSort();
alphaIndexer.put(name, i);
sections[i] = name;
}
}
}
@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;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.alpha = (TextView) convertView.findViewById(R.id.alpha);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.view_item_top_line =convertView.findViewById(R.id.view_item_top_line);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(list.get(position).getCityName());
holder.code.setText(list.get(position).getAreaCode());
String currentStr = list.get(position).getNameSort();
String previewStr = (position - 1) >= 0 ? list.get(position - 1).getNameSort() : " ";
if (!previewStr.equals(currentStr))
{
holder.alpha.setVisibility(View.VISIBLE);
holder.alpha.setText(currentStr);
holder.view_item_top_line.setVisibility(View.GONE);
} else
{
holder.alpha.setVisibility(View.GONE);
holder.view_item_top_line.setVisibility(View.VISIBLE);
}
return convertView;
}
private class ViewHolder
{
TextView alpha;
TextView name;
TextView code;
View view_item_top_line;
}
}
// 初始化汉语拼音首字母弹出提示框
private void initOverlay()
{
LayoutInflater inflater = LayoutInflater.from(this);
overlay = (TextView) inflater.inflate(R.layout.overlay, null);
overlay.setVisibility(View.INVISIBLE);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT);
WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
windowManager.addView(overlay, lp);
}
private class LetterListViewListener implements OnTouchingLetterChangedListener
{
@Override
public void onTouchingLetterChanged(final String s)
{
if (alphaIndexer.get(s) != null)
{
int position = alphaIndexer.get(s);
mCityLit.setSelection(position+1);
overlay.setText(sections[position]);
overlay.setVisibility(View.VISIBLE);
handler.removeCallbacks(overlayThread);
// 延迟一秒后执行,让overlay为不可见
handler.postDelayed(overlayThread, 700);
}
}
}
// 设置overlay不可见
private class OverlayThread implements Runnable
{
@Override
public void run()
{
overlay.setVisibility(View.GONE);
}
}
}
package com.city.list.main;
/**
* 城市属性实体类
* @author sy
*
*/
public class CityModel
{
private String CityName; //城市名字
private String NameSort; //城市首字母
private String AreaCode; //地区编号
public String getAreaCode() {
return AreaCode;
}
public void setAreaCode(String areaCode) {
AreaCode = areaCode;
}
public String getCityName()
{
return CityName;
}
public void setCityName(String cityName)
{
CityName = cityName;
}
public String getNameSort()
{
return NameSort;
}
public void setNameSort(String nameSort)
{
NameSort = nameSort;
}
}
package com.city.list.main;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MyLetterListView extends View
{
OnTouchingLetterChangedListener onTouchingLetterChangedListener;
String[] b = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","#" };
int choose = -1;
Paint paint = new Paint();
boolean showBkg = false;
public MyLetterListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public MyLetterListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MyLetterListView(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if (showBkg)
{
//canvas.drawColor(Color.parseColor("#f3c516"));
}
int height = getHeight();
int width = getWidth();
int singleHeight = height / b.length;
for (int i = 0; i < b.length; i++)
{
paint.setColor(Color.parseColor("#f3c516"));
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
if (i == choose)
{
paint.setColor(Color.parseColor("#f3c516"));
paint.setFakeBoldText(true);
}
float xPos = width / 2 - paint.measureText(b[i]) / 2;
float yPos = singleHeight * i + singleHeight;
paint.setTextSize(35);
canvas.drawText(b[i], xPos, yPos, paint);
paint.reset();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event)
{
final int action = event.getAction();
final float y = event.getY();
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y / getHeight() * b.length);
switch (action) {
case MotionEvent.ACTION_DOWN:
showBkg = true;
if (oldChoose != c && listener != null)
{
if (c >= 0 && c < b.length)
{
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_MOVE:
if (oldChoose != c && listener != null)
{
if (c >= 0 && c < b.length)
{
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
showBkg = false;
choose = -1;
invalidate();
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
return super.onTouchEvent(event);
}
public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener)
{
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
public interface OnTouchingLetterChangedListener
{
public void onTouchingLetterChanged(String s);
}
}
package com.city.list.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import com.city.list.main.R;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* 将raw中得数据库文件写入到data数据库中
* @author sy
*
*/
public class DBManager
{
private final int BUFFER_SIZE = 400000;
private static final String PACKAGE_NAME = "com.city.list.main";
public static final String DB_NAME = "china_city_name.db";
public static final String DB_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + PACKAGE_NAME ; // 存放路径
private Context mContext;
private SQLiteDatabase database;
public DBManager(Context context)
{
this.mContext = context;
}
/**
* 被调用方法
*/
public void openDateBase()
{
this.database = this.openDateBase(DB_PATH + "/" + DB_NAME);
}
/**
* 打开数据库
*
* @param dbFile
* @return SQLiteDatabase
* @author sy
*/
private SQLiteDatabase openDateBase(String dbFile)
{
File file = new File(dbFile);
if (!file.exists())
{
// // 打开raw中得数据库文件,获得stream流
InputStream stream = this.mContext.getResources().openRawResource(R.raw.china_city_name);
try
{
// 将获取到的stream 流写入道data中
FileOutputStream outputStream = new FileOutputStream(dbFile);
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
while ((count = stream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, count);
}
outputStream.close();
stream.close();
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
return db;
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return database;
}
public void closeDatabase()
{
if (database != null && database.isOpen())
{
this.database.close();
}
}
}
差不多了,还有一些布局文件和raw文件夹下的文件没显示,全在项目包下,已经把项目提交到了GitHub上:https://github.com/xinyitiandi/CityList,有哪些不足之处,大家可以指正哦,这次写的匆忙,没有加太多的文字说明,实在没有太多的时间了,请大家谅解!