Android 搜索筛选器空异常

Android 搜索筛选器空异常,android,listview,search,adapter,Android,Listview,Search,Adapter,每个listview列包含5textview-ID、首字母、日期、位置、病房 我试图让它通过初始值过滤listview。然而,我在第166行遇到了一个空异常 我假设editText没有按首字母过滤,似乎每次调用适配器都会导致错误消息。因此,我假设问题在于适配器 package com.example.medilearner; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity;

每个listview列包含5textview-ID、首字母、日期、位置、病房

我试图让它通过初始值过滤listview。然而,我在第166行遇到了一个空异常

我假设editText没有按首字母过滤,似乎每次调用适配器都会导致错误消息。因此,我假设问题在于适配器

  package com.example.medilearner;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.medilearner.database.patientDbAdapter;

public class PatientDetails extends Activity {

    // Database adapter
    patientDbAdapter patientDB;
    Context myContext;
    // ArrayAdapter
    ArrayAdapter<String> adapter;

    ArrayList<HashMap<String, String>> encouterlist = new ArrayList<HashMap<String, String>>();
    // Various layout
    ListView patientList;

    static String value;
    private EditText filterText = null;

    private static final String TAG_E_ID = "e_id";
    private static final String TAG_E_INITIAL = "entry_initial";
    private static final String TAG_E_DATE = "entry_date";
    private static final String TAG_E_SITELOCATION = "entry_sitelocation";
    private static final String TAG_E_WARDCLINIC = "entry_clinic";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_details);
        encouterlist = new ArrayList<HashMap<String, String>>();
        myContext = this;
        patientDB = new patientDbAdapter(myContext);
        patientDB.open();
        Cursor mCursor = patientDB.retrieveAllPatientEntriesCursor();
        int count = mCursor.getCount();
        if ((mCursor != null) && (mCursor.getCount() > 0)) {
            mCursor.moveToFirst();

            do {
                // Get the data
                // Convert the initial to String

                String ID = mCursor.getString(0);
                String Initial = mCursor.getString(1);
                String Date = mCursor.getString(2);
                String SiteLocation = mCursor.getString(3);
                String WardClinic = mCursor.getString(4);

                // Indicate that it's successful
                Log.i("Successful retrival of", Initial);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_E_ID, ID);
                map.put(TAG_E_INITIAL, Initial);
                map.put(TAG_E_DATE, "Last Diagnosis: " + Date);
                map.put(TAG_E_SITELOCATION, "Location: " + SiteLocation);
                map.put(TAG_E_WARDCLINIC, "Ward: " + WardClinic);

                // Add it to the array such that i can handle the array
                // afterwhich
                encouterlist.add(map);

            }
            // move to the next row
            while (mCursor.moveToNext());
        }

        filterText = (EditText) findViewById(R.id.patientDetailsSearch);
        filterText.addTextChangedListener(filterTextWatcher);
        // need to link the layout
        patientList = (ListView) findViewById(R.id.PatientDetailslist);




        patientList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String str = encouterlist.get(+position).get("e_id");
                String name = encouterlist.get(+position).get("entry_initial");
                patientDB.open();

                patientDB.removeEntry(str);
                patientDB.close();
        //      PatientDetails.this.recreate();

                Toast.makeText(myContext, name + " Deleted", Toast.LENGTH_SHORT).show();

                // TODO Auto-generated method stub
                return true; 
            }

        }
        );

        patientList
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        Toast.makeText(
                                PatientDetails.this,
                                encouterlist.get(+position)
                                        .get("entry_initial") + " Selected",
                                Toast.LENGTH_SHORT).show();
                        Intent localIntent = new Intent(PatientDetails.this,
                                PatientInfo.class);
                        String str = encouterlist.get(+position).get("e_id");
                        localIntent.putExtra("value", str);
                        PatientDetails.this.setResult(-1, localIntent);
                        PatientDetails.this.finish();
                    }


                }

                );
        ListAdapter adapter = new SimpleAdapter(PatientDetails.this,
                encouterlist, R.layout.list_patient, new String[] { TAG_E_ID,
                        TAG_E_INITIAL, TAG_E_DATE, TAG_E_SITELOCATION,
                        TAG_E_WARDCLINIC }, new int[] { R.id.eid,
                        R.id.epatientInitial, R.id.epatientDate,
                        R.id.epatientSiteLocation, R.id.epatientWardClinci });

    this.patientList.setAdapter(adapter);


        };

        private TextWatcher filterTextWatcher = new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                adapter.getFilter().filter(s);
            }

        };


    protected void onDestroy() {
        super.onDestroy();
        filterText.removeTextChangedListener(filterTextWatcher);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.patient_details, menu);
        return true;
    }

}

您可以创建列表适配器的本地实例

ListAdapter adapter = new SimpleAdapter(PatientDetails.this ....
在文本观察程序中,您使用的是memeber
适配器

试着换成

this.adapter = new SimpleAdapter(PatientDetails.this ....

您可以创建列表适配器的本地实例

ListAdapter adapter = new SimpleAdapter(PatientDetails.this ....
在文本观察程序中,您使用的是memeber
适配器

试着换成

this.adapter = new SimpleAdapter(PatientDetails.this ....
这是因为

public void onTextChanged(CharSequence s, int start, int before,
                int count) {
    adapter.getFilter().filter(s);
}
您的
适配器
为空。您正在本地创建
ListAdapter
,这在范围之外没有意义。您可以使用全局适配器,而不是在本地创建
ListAdapter

这是因为

public void onTextChanged(CharSequence s, int start, int before,
                int count) {
    adapter.getFilter().filter(s);
}

您的
适配器
为空。您正在本地创建
ListAdapter
,这在范围之外没有意义。您可以使用全局适配器,而不是在本地创建
ListAdapter

您好,请查看代码示例

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

    // List view
    private ListView lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;


    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Listview Data
        String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                                "iPhone 4S", "Samsung Galaxy Note 800",
                                "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);

        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                MainActivity.this.adapter.getFilter().filter(cs);   
            }

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

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                          
            }
        });
    }    
}
导入android.app.Activity;
导入android.os.Bundle;
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.widget.ArrayAdapter;
导入android.widget.EditText;
导入android.widget.ListView;
公共类MainActivity扩展了活动{
//列表视图
私有ListView lv;
//列表视图适配器
阵列适配器;
//搜索编辑文本
编辑文本输入搜索;
//Listview的ArrayList
ArrayList产品列表;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//列表视图数据
字符串产品[]={“戴尔Inspiron”、“HTC One X”、“HTC Wildfire S”、“HTC Sense”、“HTC Sensation XE”,
“iPhone 4S”、“三星Galaxy Note 800”,
“三星Galaxy S3”、“MacBook Air”、“MacMini”、“MacBook Pro”};
lv=(ListView)findViewById(R.id.list\u视图);
inputSearch=(EditText)findViewById(R.id.inputSearch);
//向listview添加项目
适配器=新阵列适配器(此,R.layout.list_项,R.id.product_名称,产品);
低压设置适配器(适配器);
/**
*启用搜索筛选器
* */
inputSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列cs、int arg1、int arg2、int arg3){
//当用户更改文本时
MainActivity.this.adapter.getFilter().filter(cs);
}
@凌驾
更改前的公共void(字符序列arg0、int arg1、int arg2、,
int arg3){
//TODO自动生成的方法存根
}
@凌驾
public void PostTextChanged(可编辑arg0){
//TODO自动生成的方法存根
}
});
}    
}
activity_main.xml

<?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="fill_parent"
    android:orientation="vertical" >

    <!-- Editext for Search -->
    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Search products.."
        android:inputType="textVisiblePassword"/>

    <!-- List View -->
    <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

list_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" >

    <!-- Single ListItem -->

    <!-- Product Name -->
    <TextView android:id="@+id/product_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold"/>    

</LinearLayout>

您好,请浏览代码示例

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

    // List view
    private ListView lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;


    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Listview Data
        String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                                "iPhone 4S", "Samsung Galaxy Note 800",
                                "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);

        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                MainActivity.this.adapter.getFilter().filter(cs);   
            }

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

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                          
            }
        });
    }    
}
导入android.app.Activity;
导入android.os.Bundle;
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.widget.ArrayAdapter;
导入android.widget.EditText;
导入android.widget.ListView;
公共类MainActivity扩展了活动{
//列表视图
私有ListView lv;
//列表视图适配器
阵列适配器;
//搜索编辑文本
编辑文本输入搜索;
//Listview的ArrayList
ArrayList产品列表;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//列表视图数据
字符串产品[]={“戴尔Inspiron”、“HTC One X”、“HTC Wildfire S”、“HTC Sense”、“HTC Sensation XE”,
“iPhone 4S”、“三星Galaxy Note 800”,
“三星Galaxy S3”、“MacBook Air”、“MacMini”、“MacBook Pro”};
lv=(ListView)findViewById(R.id.list\u视图);
inputSearch=(EditText)findViewById(R.id.inputSearch);
//向listview添加项目
适配器=新阵列适配器(此,R.layout.list_项,R.id.product_名称,产品);
低压设置适配器(适配器);
/**
*启用搜索筛选器
* */
inputSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列cs、int arg1、int arg2、int arg3){
//当用户更改文本时
MainActivity.this.adapter.getFilter().filter(cs);
}
@凌驾
更改前的公共void(字符序列arg0、int arg1、int arg2、,
int arg3){
//TODO自动生成的方法存根
}
@凌驾
public void PostTextChanged(可编辑arg0){
//TODO自动生成的方法存根
}
});
}    
}
activity_main.xml

<?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="fill_parent"
    android:orientation="vertical" >

    <!-- Editext for Search -->
    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Search products.."
        android:inputType="textVisiblePassword"/>

    <!-- List View -->
    <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

list_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" >

    <!-- Single ListItem -->

    <!-- Product Name -->
    <TextView android:id="@+id/product_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold"/>    

</LinearLayout>