相信很多人都有看过或了解过城市列表和联系人排序这种效果,下面来直接看看我今天要实现的效果:
可以进行城市的搜索,效果很实用,直接上代码:源码下载 点击下载源码
1.主函数:
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Toast;
import com.liucanwen.citylist.adapter.CityAdapter;
import com.liucanwen.citylist.data.CityData;
import com.liucanwen.citylist.model.CityItem;
import com.liucanwen.citylist.widget.ContactItemInterface;
import com.liucanwen.citylist.widget.ContactListViewImpl;
public class CityListActivity extends Activity implements TextWatcher
{
private Context context_ = CityListActivity.this;
private ContactListViewImpl listview;
private EditText searchBox;
private String searchString;
private Object searchLock = new Object();
boolean inSearchMode = false;
private final static String TAG = "MainActivity2";
List<ContactItemInterface> contactList;
List<ContactItemInterface> filterList;
private SearchListTask curSearchTask = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.citylist);
filterList = new ArrayList<ContactItemInterface>();
contactList = CityData.getSampleContactList();
CityAdapter adapter = new CityAdapter(this,R.layout.city_item, contactList);
listview = (ContactListViewImpl) this.findViewById(R.id.listview);
listview.setFastScrollEnabled(true);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView parent, View v, int position,
long id)
{
List<ContactItemInterface> searchList = inSearchMode ? filterList
: contactList;
Toast.makeText(context_,
searchList.get(position).getDisplayInfo()+"9",
Toast.LENGTH_SHORT).show();
}
});
searchBox = (EditText) findViewById(R.id.input_search_query);
searchBox.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable s)
{
searchString = searchBox.getText().toString().trim().toUpperCase();
if (curSearchTask != null
&& curSearchTask.getStatus() != AsyncTask.Status.FINISHED)
{
try
{
curSearchTask.cancel(true);
} catch (Exception e)
{
Log.i(TAG, "Fail to cancel running search task");
}
}
curSearchTask = new SearchListTask();
curSearchTask.execute(searchString);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// do nothing
}
private class SearchListTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
filterList.clear();
String keyword = params[0];
inSearchMode = (keyword.length() > 0);
if (inSearchMode)
{
// get all the items matching this
for (ContactItemInterface item : contactList)
{
CityItem contact = (CityItem) item;
boolean isPinyin = contact.getFullName().toUpperCase().indexOf(keyword) > -1;
boolean isChinese = contact.getNickName().indexOf(keyword) > -1;
if (isPinyin || isChinese)
{
filterList.add(item);
}
}
}
return null;
}
protected void onPostExecute(String result)
{
synchronized (searchLock)
{
if (inSearchMode)
{
CityAdapter adapter = new CityAdapter(context_,R.layout.city_item, filterList);
adapter.setInSearchMode(true);
listview.setInSearchMode(true);
listview.setAdapter(adapter);
} else
{
CityAdapter adapter = new CityAdapter(context_,R.layout.city_item, contactList);
adapter.setInSearchMode(false);
listview.setInSearchMode(false);
listview.setAdapter(adapter);
}
}
}
}
2.适配器:
import java.util.List;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import com.liucanwen.citylist.R;
import com.liucanwen.citylist.widget.ContactItemInterface;
import com.liucanwen.citylist.widget.ContactListAdapter;
public class CityAdapter extends ContactListAdapter
{
public CityAdapter(Context _context, int _resource,
List<ContactItemInterface> _items)
{
super(_context, _resource, _items);
}
public void populateDataForRow(View parentView, ContactItemInterface item,
int position)
{
View infoView = parentView.findViewById(R.id.infoRowContainer);
TextView nicknameView = (TextView) infoView
.findViewById(R.id.cityName);
nicknameView.setText(item.getDisplayInfo());
}
}
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.liucanwen.citylist.model.CityItem;
import com.liucanwen.citylist.widget.ContactItemInterface;
import com.liucanwen.citylist.widget.pinyin.PinYin;
/**
*
* @author sgf
*
*/
public class CityData
{
static String cityJson = "{'cities':['#','#热门城市','上海','北京','杭州','广州','南京','苏州','深圳','成都','重庆','天津','宁波','扬州','无锡','福州','厦门','武汉','西安','沈阳','大连','青岛','济南','海口','石家庄','唐山','秦皇岛','邯郸','邢台','保定','张家口','承德','沧州','廊坊','衡水','太原','大同','阳泉','长治','晋城','朔州','晋中','运城','忻州','临汾','吕梁','呼和浩特','包头','乌海','赤峰','通辽','鄂尔多斯','呼伦贝尔','兴安盟','锡林郭勒','乌兰察布','巴彦淖尔','阿拉善','鞍山','抚顺','本溪','丹东','锦州','营口','阜新','辽阳','盘锦','铁岭','朝阳','葫芦岛','长春','吉林','四平','辽源','通化','白山','松原','白城','延边','哈尔滨','齐齐哈尔','鸡西','鹤岗','双鸭山','大庆','伊春','佳木斯','七台河','牡丹江','黑河','绥化','大兴安岭','徐州','常州','南通','连云港','淮安','盐城','镇江','泰州','宿迁','温州','嘉兴','湖州','绍兴','金华','衢州','舟山','台州','丽水','合肥','芜湖','蚌埠','淮南','马鞍山','淮北','铜陵','安庆','黄山','滁州','阜阳','宿州','六安','亳州','池州','宣城','莆田','三明','泉州','漳州','南平','龙岩','宁德','南昌','景德镇','萍乡','九江','新余','鹰潭','赣州','吉安','宜春','抚州','上饶','淄博','枣庄','东营','烟台','潍坊','济宁','泰安','威海','日照','莱芜','临沂','德州','聊城','滨州','菏泽','郑州','开封','洛阳','平顶山','安阳','鹤壁','新乡','焦作','濮阳','许昌','漯河','三门峡','南阳','商丘','信阳','周口','驻马店','黄石','十堰','宜昌','襄阳','鄂州','荆门','孝感','荆州','黄冈','咸宁','随州','恩施州','仙桃','潜江','天门','株洲','湘潭','衡阳','邵阳','岳阳','常德','张家界','益阳','郴州','永州','怀化','娄底','湘西','韶关','珠海','汕头','佛山','江门','湛江','茂名','肇庆','惠州','梅州','汕尾','河源','阳江','清远','东莞','中山','潮州','揭阳','云浮','南宁','柳州','桂林','梧州','北海','防城港','钦州','贵港','玉林','百色','贺州','河池','自贡','攀枝花','泸州','德阳','绵阳','广元','遂宁','内江','乐山','南充','眉山','宜宾','广安','达州','雅安','巴中','资阳','阿坝','甘孜州','凉山','贵阳','六盘水','遵义','安顺','铜仁地区','黔西南','毕节地区','黔东南','黔南','昆明','曲靖','玉溪','保山','昭通','楚雄州','红河','文山州','普洱','西双版纳','大理州','德宏','丽江','怒江','迪庆','临沧','拉萨','昌都地区','山南','日喀则地区','那曲','阿里','林芝地区','铜川','宝鸡','咸阳','渭南','延安','汉中','榆林','安康','商洛','兰州','嘉峪关','金昌','白银','天水','武威','张掖','平凉','酒泉','庆阳','定西','陇南','临夏州','甘南','西宁','海东','海北','黄南','果洛','玉树','海西','银川','石嘴山','吴忠','固原','乌鲁木齐','克拉玛依','吐鲁番地区','哈密地区','昌吉州','博尔塔拉','巴音郭楞','阿克苏地区','克孜勒苏','喀什地区','和田地区','伊犁','塔城地区','阿勒泰地区','石河子','香港','澳门','长沙','三亚','中卫','儋州','保亭','昌江','澄迈县','崇左','定安县','东方','济源','来宾','乐东','陵水','琼海','神农架林区','图木舒克','屯昌县','万宁','文昌','海南州']}";
public static List<ContactItemInterface> getSampleContactList()
{
List<ContactItemInterface> list = new ArrayList<ContactItemInterface>();
try
{
JSONObject jo1 = new JSONObject(cityJson);
JSONArray ja1 = jo1.getJSONArray("cities");
for(int i = 0; i < ja1.length(); i++)
{
String cityName = ja1.getString(i);
list.add(new CityItem(cityName, PinYin.getPinYin(cityName)));
}
}
catch (JSONException e)
{
e.printStackTrace();
}
return list;
}
}
import com.liucanwen.citylist.widget.ContactItemInterface;
public class CityItem implements ContactItemInterface
{
private String nickName;
private String fullName;
public CityItem(String nickName, String fullName)
{
super();
this.nickName = nickName;
this.setFullName(fullName);
}
@Override
public String getItemForIndex()
{
return fullName;
}
@Override
public String getDisplayInfo()
{
return nickName;
}
public String getNickName()
{
return nickName;
}
public void setNickName(String nickName)
{
this.nickName = nickName;
}
public String getFullName()
{
return fullName;
}
public void setFullName(String fullName)
{
this.fullName = fullName;
}
}
5.还有不少辅助类我就粘贴出来了,我提供了源码,亲可以直接下载,下面看看布局:city_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/sectionTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dip"
android:background="#496491"
android:paddingLeft="8dip"
android:text="A"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/infoRowContainer"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="@drawable/listitem_selector"
android:gravity="center_vertical"
android:paddingLeft="10dip" >
<TextView
android:id="@+id/cityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="广州"
android:textSize="20dip"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
6.citylist.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<RelativeLayout
android:id="@+id/searchBarContainer"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="@drawable/search_bg" >
<EditText
android:id="@+id/input_search_query"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_margin="5dip"
android:background="@drawable/search_puin"
android:imeOptions="actionDone"
android:maxLines="1"
android:paddingLeft="5dip"
android:paddingRight="25dip"
android:singleLine="true"
android:text=""
android:textSize="15dip" />
<ImageButton
android:id="@+id/button_search"
style="@android:style/Widget.Button.Inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"
android:background="@drawable/icon_search"
android:contentDescription="Search..."
android:scaleType="fitCenter" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/searchBarContainer" >
<com.liucanwen.citylist.widget.ContactListViewImpl
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:cacheColorHint="#00000000"
android:divider="@drawable/line"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
android:listSelector="#00000000"
android:smoothScrollbar="true" />
</LinearLayout>
</RelativeLayout>
OK,不清楚的可以直接下载源码 点击下载源码