Searchbox in Listview: always the same result

我的梦境 提交于 2019-12-11 19:50:15

问题


when i use the serachbox for my Listview, i get everytime the same result. For Example:

Car Flower Hate Love Water

When I typef or h or l etc. the result is Car. I worked with this example http://androidcocktail.blogspot.in/2012/04/search-custom-listview-in-android.html

Activity:

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;
        }
    }
}

DatabaseHandler:

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>

I uploaded the files as eclipse workspace: http://www.file-upload.net/download-4447258/test.rar.html


回答1:


Are you sure this line works?:

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

Maybe try this instead:

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




回答2:


I found the problem. In the Activity the last three viewHolder worked with antidots and not with antidotsResult

Wrong:

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());

Works:

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());


来源:https://stackoverflow.com/questions/11040117/searchbox-in-listview-always-the-same-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!