如何添加Searchable Editbox?

时间:2014-10-24 14:41:42

标签: android

我正在尝试创建一个可搜索的edittext。 但是没有在log cat中显示任何错误。 它不显示搜索结果。我在androidmanifest中添加了搜索意图。任何人都可以指导我解决这个问题。

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

private MyDBHelper mydb;
private SimpleCursorAdapter adapter;
public static final String Row_ID = "row_id"; 
EditText inputSearch;
ListView dplist;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //********************************//
    //Begin of Database-Table1_Dept_List
    //********************************//
    dplist= (ListView) findViewById(R.id.list_view);
    //Instantiate database
    mydb =new MyDBHelper(this, null, null,4);
    SQLiteDatabase db =mydb.getReadableDatabase();
    //Removing duplicate values from table1_dept_list
    mydb.delete_dep_list();
    //Inserting into table1_dept_list
    mydb.add_dept_list("DLBIOCHEM","BioChemistry");
    mydb.add_dept_list("DLBIOTECH","BioTechnology");
    mydb.add_dept_list("DLBOT", "Botany");
    mydb.add_dept_list("DLCHEM","Chemistry");
    mydb.add_dept_list("DLCOM","Commerce");
    mydb.add_dept_list("DLCS","Computer Science");
    mydb.add_dept_list("DLECO","Economics");
    mydb.add_dept_list("DLEDU","Education");
    mydb.add_dept_list("DLENG","English");
    mydb.add_dept_list("DLEVS","Environmental Science");
    mydb.add_dept_list("DLFSN","Food Science and Nutrition");
    mydb.add_dept_list("DLGEO","Geology");
    mydb.add_dept_list("DLJMC", "Journalism and Massmedia Communication");
    mydb.add_dept_list("DLLIS","Library and Information Science");
    mydb.add_dept_list("DLMATH","Mathematics");
    mydb.add_dept_list("DLMICRO","Microbiology");
    mydb.add_dept_list("DLPE","Physical Education");
    mydb.add_dept_list("DLPHY","Physics");
    mydb.add_dept_list("DLPRIMS","Periyar Institute of Management Studies");
    mydb.add_dept_list("DLPSY","Psychology");
    mydb.add_dept_list("DLSOC","Sociology");
    mydb.add_dept_list("DLTAM", "Tamil");
    mydb.add_dept_list("DLTAD","Textile and Apparel Design");
    mydb.add_dept_list("DLZOO","Zoology");

    //list view 
    Cursor AllDeptList = mydb.get_dept_list();
    String[] from=new String[] { 
            MyDBHelper.DEPT_LIST_COLUMN_NAME
    };
    int[] to=new int[] {R.id.dis_text};
    adapter = new SimpleCursorAdapter(this,R.layout.disp_text,AllDeptList,from,to,0 );

    dplist.setAdapter(adapter);
    //********************************//
    //end of Database-Table1_Dept_List//
    //********************************//

    //******************************//
    //Passing values to new activity//
    //******************************//

    dplist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

   @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
    // TODO Auto-generated method stub

Cursor cursor = (Cursor) dplist.getItemAtPosition(position);
String dep_name = cursor.getString(cursor.getColumnIndex(MyDBHelper.DEPT_LIST_COLUMN_NAME));
String dep_id = cursor.getString(cursor.getColumnIndex(MyDBHelper.DEPT_LIST_COLUMN_ID));

Intent intent_dp_list = new  Intent(MainActivity.this,DepartmentDesignation.class);
intent_dp_list.putExtra("dep_name",dep_name);               
intent_dp_list.putExtra("dep_id",dep_id);
startActivity(intent_dp_list);

}

});
    //*********************************//
    //end of passing values to activity//
    //*********************************//

    inputSearch = (EditText)findViewById(R.id.search_box);
    inputSearch.addTextChangedListener(new TextWatcher(){

         @Override
         public void afterTextChanged(Editable arg0) {
             // TODO Auto-generated method stub


        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2,
                int arg3) {
             // TODO Auto-generated method stub
             MainActivity.this.adapter.getFilter().filter(cs);

         }

    });

  }

activity_main.xml中

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="horizontal" >



 <EditText android:id="@+id/search_box"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:hint="@string/searchhint" />
 <ListView android:id ="@+id/list_view"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />



 </LinearLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contact"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.contact.MainActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateHidden" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
             <action android:name="android.intent.action.SEARCH" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>


    </activity>
    <activity android:name="com.example.contact.DepartmentDesignation" />
    <activity android:name="com.example.contact.PersonsListActivity" />
    <activity android:name="com.example.contact.HodDetails" />
</application>

</manifest>

1 个答案:

答案 0 :(得分:-1)

您需要实现自定义适配器,以便在按文本过滤时显示结果。

public class Adapter extends BaseAdapter implements Filterable {

    private LayoutInflater mInflater;
    List<Items> mList;
    List<Items> mFilter;

    public Adapter(List<Items> list, Context context) {
        mInflater = LayoutInflater.from(context);
        mList = list;
        mFilter = list;
    }

    @Override
    public int getCount() {

        return mFilter.size();
    }

    @Override
    public Object getItem(int position) {
        return mFilter.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mFilter.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

  //show view
        return convertView;
    }



    /*********** SEARCH **********/
    @Override
    public Filter getFilter() {

        return new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {
                mFilter = (List<Items>) results.values;
                notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {

                FilterResults results = new FilterResults();

                if (charSequence == null || charSequence.length() == 0) {
                    results.values = mList;
                    results.count = mList.size();
                } else {
                    ArrayList<Items> filterResultsData = new ArrayList<Items>();

                    // search into database and show the results

                    results.values = filterResultsData;
                    results.count = filterResultsData.size();
                }

                return results;
            }
        };
    }

}