android EditText+ListView的组合(类似于AutoCompleteTextView)


android EditText+ListView的组合(类似于AutoCompleteTextView)

很好的帖子:

eoeWiki客户端(android版)1.0.0版正式发布~
http://www.eoeandroid.com/thread-194188-1-1.html

Android定时自动启动应用程序应用教程
http://www.eoeandroid.com/thread-194271-1-1.html

基于Android 随手记
http://www.eoeandroid.com/thread-194031-1-1.html

 

因为问这个问题的人很多,所以刚刚找了下代码,结果还手动的把他提取出来。好了废话不多说了。直接上代码。

复制代码
package ki.test.edit_list;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
 
public class DBManager {
        private final int BUFFER_SIZE = 40000;
        public static final String DB_NAME = "city.db"; // 保存的数据库文件名
        public static final String PACKAGE_NAME = "ki.test.edit_list";
        public static final String DB_PATH = "/data"
                        + Environment.getDataDirectory().getAbsolutePath() + "/"
                        + PACKAGE_NAME; // 在手机里存放数据库的位置
 
        private SQLiteDatabase database;
        private Context context;
 
        public DBManager(Context context) {
                this.context = context;
        }
 
        SQLiteDatabase db = null;
 
        public SQLiteDatabase getDatabase(){
                try {
                        openDatabase();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return db;
        }
         
        private void openDatabase() throws IOException {
                String dbfile = DB_PATH + "/" + DB_NAME;
 
                if (!(new File(dbfile).exists())) {// 判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
                        InputStream is = this.context.getResources().openRawResource(
                                        R.raw.city); // 欲导入的数据库
                        FileOutputStream fos = new FileOutputStream(dbfile);
                        byte[] buffer = new byte[BUFFER_SIZE];
                        int count = 0;
                        while ((count = is.read(buffer)) > 0) {
                                fos.write(buffer, 0, count);
                        }
                        fos.close();
                        is.close();
                }
 
                db = SQLiteDatabase.openDatabase(dbfile, null,
                                SQLiteDatabase.OPEN_READWRITE);
        }
 
        public void closeDatabase() {
                this.database.close();
        }
         
         
}
复制代码
复制代码
package ki.test.edit_list;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
 
public class SearchCity extends Activity implements OnItemClickListener, TextWatcher {
 
        public static List<Map<String, String>> City_Lists = new ArrayList<Map<String, String>>();
 
        private List<Map<String, String>> defaultLists = new ArrayList<Map<String, String>>();
         
        private List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
 
 
        private final String NAME = "name";
        private final String PARENT_NAME = "parent_name";
 
         
        // sql 语句
        private final String sql_Head = "select b.name , a.name from city a inner join (select _id ,name from city) b on a.parent_id = b._id where dtype = 'area' and a.name like '";
        private String str_Sql = "";
        private final String sql_Foot = "%'";
 
        private EditText edit_search_city;
        private SQLiteDatabase db;
        private Cursor result;
        private SearchCity city = this;
        private ListView listView;
 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.search);
 
                initControl();
                initDefaultLists();
                listView.setOnItemClickListener(this);
 
                edit_search_city.addTextChangedListener(this);
 
        }
 
        void initControl() {
                edit_search_city = (EditText) findViewById(R.id.edit_search);
                listView = (ListView) findViewById(R.id.listview);
        }
 
        private void initDefaultLists() {
                Map<String, String> map = new HashMap<String, String>();
                map.put(NAME, getString(R.string.nocity));
                defaultLists.add(map);
        }
 
        public boolean getLists(String edit_content) {
                str_Sql = sql_Head + edit_content + sql_Foot;
                db = new DBManager(city).getDatabase();
                result = db.rawQuery(str_Sql, null);
                if (result.moveToFirst()) {
                        while (!result.isAfterLast()) {
                                Map<String, String> map = new HashMap<String, String>();
                                String name = result.getString(1);
                                String parent_name = result.getString(0);
                                if (name.equals(parent_name))
                                        parent_name = "";
                                map.put(NAME, name);
                                map.put(PARENT_NAME, parent_name);
                                lists.add(map);
                                result.moveToNext();
                        }
                        City_Lists = lists;
                        result.close();
                        db.close();
                        return true;
                }
                db.close();
                return false;
        }
 
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String put_Content = City_Lists.get(position).get(NAME);
                Toast.makeText(getApplicationContext(), put_Content, 8000).show();
        }
 
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
        }
 
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
                lists.clear();
                String edit_content = edit_search_city.getText().toString();
                if (edit_content.equals("")) {
                        return;
                }
                List<Map<String, String>> dataLists = null;
                dataLists =  getLists(edit_content) ? lists : defaultLists;
                ListAdapter adapter = new SimpleAdapter(city, dataLists, R.layout.listview, new String[] { NAME, PARENT_NAME }, new int[] { R.id.txt_list_1, R.id.txt_list_2 });
                listView.setAdapter(adapter);
        }
 
        @Override
        public void afterTextChanged(Editable s) {
 
        }
}
复制代码
复制代码
package ki.test.edit_list;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
 
public class TestDemoActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
 
                startActivity(new Intent(getApplicationContext(), SearchCity.class));
        }
}
复制代码
复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="55px"
    android:orientation="horizontal" >
 
    <TextView
        android:id="@+id/txt_list_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8px"
        android:layout_marginRight="10px"
        android:textColor="#FFFFFF"
        android:textSize="30px" />
 
    <TextView
        android:layout_width="20px"
        android:layout_height="wrap_content"
        android:layout_marginRight="5px"
        android:text="--"
        android:textSize="20px" />
 
    <TextView
        android:id="@+id/txt_list_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25px" />
 
</LinearLayout>
复制代码
复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="400px"
    android:layout_height="500px"
    android:orientation="vertical" >
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="2px"
        android:background="#CCCCCC"
        android:layout_marginBottom="5px" />
 
    <EditText
        android:id="@+id/edit_search"
        android:layout_width="350px"
        android:layout_height="wrap_content"
        android:hint="Enter the city"/>
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="2px"
        android:background="#CCCCCC"
        android:layout_marginBottom="5px" />
     
   <LinearLayout
       android:id="@+id/linear_list"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <ListView
           android:id="@+id/listview"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"></ListView>
        
   </LinearLayout>
 
</LinearLayout>
复制代码
复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <string name="hello">Hello World, TestDemoActivity!</string>
    <string name="app_name">设置城市</string>
    <string name="nocity">没有找到你想查询的城市</string>
 
</resources>
复制代码

其中由于是刚刚急着赶出来的 所以可能代码有些乱,但是主要看思路,另外我把源码 也上传了下。有不足地方请见晾。

 

 源码下载:TestDemo.rar


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值