0

当我将 serachbox 用于我的 Listview 时,我每次都会得到相同的结果。例如:

汽车 花 恨 爱 水

当我输入 f 或 h 或 l 等时,结果是 Car。我使用这个例子http://androidcocktail.blogspot.in/2012/04/search-custom-listview-in-android.html

活动:

package de.bodprod.rettinfo;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class AntidotList extends ListActivity{
    ArrayList<HashMap<String, Object>> antidots;
    ArrayList<HashMap<String, Object>> antidotsResult;
    LayoutInflater inflater;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.antidotlist_layout);
        final EditText searchBox=(EditText) findViewById(R.id.EditText01);
        ListView antidotListView=(ListView) findViewById(android.R.id.list);

        inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        antidots = new ArrayList<HashMap<String,Object>>();        
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
        antidots = db.getAllAntidots();

        antidotsResult = new ArrayList<HashMap<String,Object>>(antidots);

        final CustomAdapter adapter = new CustomAdapter(this, R.layout.antidotlist_layout, antidotsResult);
        antidotListView.setAdapter(adapter);

        searchBox.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //get the text in the EditText
                String searchString=searchBox.getText().toString();
                int textLength=searchString.length();

                antidotsResult.clear();

                for(int i=0;i<antidots.size();i++){
                    String toxinname=antidots.get(i).get("toxin").toString();
                    if(textLength<=toxinname.length()){
                        if(searchString.equalsIgnoreCase(toxinname.substring(0,textLength)))
                            antidotsResult.add(antidots.get(i));
                    }
                } 
                adapter.notifyDataSetChanged();
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            public void afterTextChanged(Editable s) {}
        });
    }

    private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>{
        public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
            super(context, textViewResourceId, Strings);
        }
        private class ViewHolder{
            TextView sqlite_id, tox_layout, antidot_layout;
        }

        ViewHolder viewHolder;

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){        
                convertView=inflater.inflate(R.layout.antidotlistitem_layout, null);
                viewHolder=new ViewHolder();

                viewHolder.sqlite_id=(TextView) convertView.findViewById(R.id.sqlite_id);
                viewHolder.tox_layout=(TextView) convertView.findViewById(R.id.tox_layout);
                viewHolder.antidot_layout=(TextView) convertView.findViewById(R.id.antidot_layout);

                convertView.setTag(viewHolder);

            }else{
                viewHolder=(ViewHolder) convertView.getTag();
            }

            viewHolder.sqlite_id.setText(antidots.get(position).get("sql_id").toString());
            viewHolder.tox_layout.setText(antidots.get(position).get("toxin").toString());
            viewHolder.antidot_layout.setText(antidots.get(position).get("antidot").toString());

            return convertView;
        }
    }
}

数据库处理器:

package de.bodprod.rettinfo;

import java.util.ArrayList;
import java.util.HashMap;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 7;

    // Database Name
    private static final String DATABASE_NAME = "rettinfo";

    // Antidot table name
    private static final String TABLE_ANTIDOT = "antidotlist";

    // Antidot Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_TOX = "tox";
    private static final String KEY_ANTIDOT = "antidot";
    private static final String KEY_DOS = "dos";
    private static final String KEY_KAT = "kat";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("Create: ", "Creating ..");
        String CREATE_ANTIDOT_TABLE = "CREATE TABLE " + TABLE_ANTIDOT + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TOX + " TEXT,"
                + KEY_ANTIDOT + " TEXT," + KEY_DOS + " TEXT," + KEY_KAT + " INTEGER" + ")";
        Log.d("Create: ", CREATE_ANTIDOT_TABLE);
        db.execSQL(CREATE_ANTIDOT_TABLE);

        ContentValues values = new ContentValues();
        values.put(KEY_TOX, "Alkylphosphat");
        values.put(KEY_ANTIDOT, "Atropinsulfat");
        values.put(KEY_DOS, "Initial: 2 - 5 mg i.V. alle 10 bis 15 Min &#xA; Kinder - Initial: 0,5 - 2 mg i.V. &#xA; bis zum Rückgang der Bronchialsekretion");
        values.put(KEY_KAT, "1");
        db.insert(TABLE_ANTIDOT, null, values);

        values.put(KEY_TOX, "E605 (Alkylphosphat)");
        values.put(KEY_ANTIDOT, "Atropinsulfat");
        values.put(KEY_DOS, "Initial: 2 - 5 mg i.V. alle 10 bis 15 Min &#xA; Kinder - Initial: 0,5 - 2 mg i.V. &#xA; bis zum Rückgang der Bronchialsekretion");
        values.put(KEY_KAT, "1");
        db.insert(TABLE_ANTIDOT, null, values);

        values.put(KEY_TOX, "DDVP (Alkylphosphat)");
        values.put(KEY_ANTIDOT, "Atropinsulfat");
        values.put(KEY_DOS, "Initial: 2 - 5 mg i.V. alle 10 bis 15 Min &#xA; Kinder - Initial: 0,5 - 2 mg i.V. &#xA; bis zum Rückgang der Bronchialsekretion");
        values.put(KEY_KAT, "1");
        db.insert(TABLE_ANTIDOT, null, values);       

        values.put(KEY_TOX, "Sarin (Alkylphosphat)");
        values.put(KEY_ANTIDOT, "Atropinsulfat");
        values.put(KEY_DOS, "Initial: 2 - 5 mg i.V. alle 10 bis 15 Min &#xA; Kinder - Initial: 0,5 - 2 mg i.V. &#xA; bis zum Rückgang der Bronchialsekretion");
        values.put(KEY_KAT, "1");
        db.insert(TABLE_ANTIDOT, null, values);         

        values.put(KEY_TOX, "Nervengas (Alkylphosphat)");
        values.put(KEY_ANTIDOT, "Atropinsulfat");
        values.put(KEY_DOS, "Initial: 2 - 5 mg i.V. alle 10 bis 15 Min &#xA; Kinder - Initial: 0,5 - 2 mg i.V. &#xA; bis zum Rückgang der Bronchialsekretion");
        values.put(KEY_KAT, "1");
        db.insert(TABLE_ANTIDOT, null, values);         
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ANTIDOT);
        // Create tables again
        onCreate(db);
    }   


    public ArrayList<HashMap<String,Object>> getAllAntidots() {
        ArrayList<HashMap<String,Object>> antidotList = new ArrayList<HashMap<String,Object>>();
        String selectQuery = "SELECT  * FROM " + TABLE_ANTIDOT + " ORDER BY " + KEY_TOX;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        HashMap<String , Object> antidot;
        if (cursor.moveToFirst()) {
            do {
                antidot = new HashMap<String, Object>();
                antidot.put("sql_id", Integer.parseInt(cursor.getString(0)));
                antidot.put("toxin", cursor.getString(1));
                antidot.put("antidot", cursor.getString(2));
                antidotList.add(antidot);
            } while (cursor.moveToNext());
        }
        return antidotList;
    }    
}

antidotlist_layout.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"
    style="@style/mainView" 
    android:orientation="vertical" >
    <EditText android:id="@+id/EditText01"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:hint="Suchen">                               
    </EditText>    
    <ListView
        android:id="@android:id/list"
        style="@style/mainView" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>           
</LinearLayout>  

antidotlistitem_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    style="@style/mainView" 
    android:padding="5dip">

    <!-- SQLite row id / hidden by default -->
    <TextView android:id="@+id/sqlite_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
        <!-- android:visibility="gone" -->


    <!-- web site title -->
    <TextView
        android:id="@+id/tox_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dip"
        android:paddingLeft="8dip"
        android:paddingBottom="4dip"
        android:textSize="18dip"
        android:textColor="#1a1a1a" />

    <!-- web site url -->
    <TextView android:id="@+id/antidot_layout"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:paddingLeft="8dip"
        android:paddingBottom="6dip"
        android:textSize="15dip"
        android:textColor="#c00300"
        android:layout_below="@id/tox_layout"/>
</RelativeLayout>

我将文件上传为 Eclipse 工作区:http ://www.file-upload.net/download-4447258/test.rar.html

4

2 回答 2

0

Are you sure this line works?:

if(searchString.equalsIgnoreCase(toxinname.substring(0,textLength))){

Maybe try this instead:

if(toxiname.toUpperCase().indexOf(searchString.toUpperCase()) != -1)

于 2012-06-14T19:45:40.807 回答
0

我发现了问题。在 Activity 中,最后三个 viewHolder 使用了 antidots 而不是 antidotsResult

错误的:

viewHolder.sqlite_id.setText(antidots.get(position).get("sql_id").toString());
viewHolder.tox_layout.setText(antidots.get(position).get("toxin").toString());
viewHolder.antidot_layout.setText(antidots.get(position).get("antidot").toString());

作品:

viewHolder.sqlite_id.setText(antidotsResult.get(position).get("sql_id").toString());
viewHolder.tox_layout.setText(antidotsResult.get(position).get("toxin").toString());
viewHolder.antidot_layout.setText(antidotsResult.get(position).get("antidot").toString());
于 2012-06-16T02:57:48.903 回答