具有搜索功能的Android Listview

时间:2014-07-17 07:16:21

标签: android listview search

我已关注此example

但不幸的是我的搜索功能不能像我预期的那样工作(或者像他的视频一样)。

这是我的数据:

public void insertSomeItmes() {  

createItmes("850007","A B S","MONTAIGU","02 51 94 18 46");
createItmes("850012","SARL A D I P A"," SAINT HERBLAIN CEDEX","02 40 92 21 2");
createItmes("850024","AIZENAY AUTO","AIZENAY","02 51 94 60 40");    
createItmes("850034","AMS","OLONNE SUR MER","02 51 95 05 23");
createItmes("850042","ETS ARMOUET","LES HERBIERS","02 51 92 98 59");
 }  

当我输入lettre“p”时,它显示A B S,但是当我调试它时,过滤功能正常工作。 这意味着它返回SARL A D I P A. 为什么它显示A B S,而不是SARL A D I P A. 我错过了什么吗?

这是我的主要活动类:

public class MainActivity extends Activity {

    ListView lv1;
    EditText inputSearch;
    ArrayList<Client_Pers> Client_Pers_arraylist = new ArrayList<Client_Pers>();
    CustomListAdapter adapter;
    SQLController   dbcon;

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

        lv1  = (ListView) findViewById(R.id.theclient);
        inputSearch  = (EditText)findViewById(R.id.editText_Search);

        dbcon = new SQLController(this);
        dbcon.open();

       dbcon.insertSomeItmes(); 

        // Pass results to ListViewAdapter Class

        adapter = new CustomListAdapter(this, dbcon.ReadData());

        // Binds the Adapter to the ListView
        lv1.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
                 String text = inputSearch.getText().toString().toLowerCase(Locale.getDefault());
                 adapter.filter(text);

            }

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

        dbcon.close();

    } 

我的控制器:

public class SQLController {

    private DBHelper dbhelper;
    private Context ourcontext;
    private SQLiteDatabase database;
    private ArrayList<Client_Pers> results = new ArrayList<Client_Pers>();

    public SQLController(Context c) {
        ourcontext = c;
    }

    public SQLController open() throws SQLException {
        dbhelper = new DBHelper(ourcontext);
        database = dbhelper.getWritableDatabase();
        return this;
    }

    public void close() {
        dbhelper.close();
    }

    public ArrayList<Client_Pers> ReadData() {
            try {               

                String SQL_syntax = "SELECT * FROM " +  DBHelper.tableName;
//              if (FilterName != null && !FilterName.isEmpty())
//              {SQL_syntax = "SELECT * FROM " + DBHelper.tableName + "WHERE "+ DBHelper.KEY_RsNom + "LIKE '%" + FilterName + "%'";}

                Cursor c = database.rawQuery(SQL_syntax, null);
                if (c != null ) {
                    if  (c.moveToFirst()) {
                        do {
                            Client_Pers ClientItem = new Client_Pers();

                            String LeCodeClient = c.getString(c.getColumnIndex(DBHelper.KEY_CodeClient));
                            ClientItem.setLeCodeCl(LeCodeClient);

                            String LeRsNom = c.getString(c.getColumnIndex(DBHelper.KEY_RsNom));
                            ClientItem.setLeRsNom(LeRsNom);

                            String LeVille = c.getString(c.getColumnIndex(DBHelper.KEY_Ville));
                            ClientItem.setLaVille(LeVille);

                            String LeTel = c.getString(c.getColumnIndex(DBHelper.KEY_Tel));
                            ClientItem.setLeTel(LeTel);

                            results.add(ClientItem);
                        }while (c.moveToNext());
                    } 
                }
            } catch (SQLiteException se ) { 
                Log.e(getClass().getSimpleName(), "Could not create or Open the database");
            }
            return results;
        }

    public long createItmes(String LeCodeClient, String LeRsNom, String LeVille, String LeTel) {

      ContentValues initialValues = new ContentValues();
      initialValues.put(DBHelper.KEY_CodeClient, LeCodeClient);
      initialValues.put(DBHelper.KEY_RsNom, LeRsNom);
      initialValues.put(DBHelper.KEY_Ville, LeVille);
      initialValues.put(DBHelper.KEY_Tel, LeTel);


      return database.insert(DBHelper.tableName, null, initialValues);
     }

    public void insertSomeItmes() {  
    createItmes("850007","A B S","MONTAIGU","02 51 94 18 46");
    createItmes("850012","SARL A D I P A"," SAINT HERBLAIN CEDEX","02 40 92 21 2");
    createItmes("850024","AIZENAY AUTO","AIZENAY","02 51 94 60 40");    
    createItmes("850034","AMS","OLONNE SUR MER","02 51 95 05 23");
    createItmes("850042","ETS ARMOUET","LES HERBIERS","02 51 92 98 59");
     }  
}

我的自定义列表类:

public class CustomListAdapter extends BaseAdapter {

    Context mContext;
    private List<Client_Pers> Client_Pers_list = null;
    private ArrayList<Client_Pers> Client_Pers_arraylist;
    private LayoutInflater layoutInflater;

    public CustomListAdapter(Context context,List<Client_Pers> Client_Pers_list) {
        mContext = context;
        this.Client_Pers_list = Client_Pers_list; 
        layoutInflater = LayoutInflater.from(mContext);
        this.Client_Pers_arraylist = new ArrayList<Client_Pers>();
        this.Client_Pers_arraylist.addAll(Client_Pers_list);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Client_Pers_list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return Client_Pers_list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
            holder = new ViewHolder();
            holder.CodeClView = (TextView) convertView.findViewById(R.id.CodeCL);
            holder.RsNomView = (TextView) convertView.findViewById(R.id.RsNom);
            holder.VilleView = (TextView) convertView.findViewById(R.id.LaVille);
            holder.TelView = (TextView) convertView.findViewById(R.id.TheTel);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        Client_Pers ClientItem = (Client_Pers)Client_Pers_arraylist.get(position);
        holder.CodeClView.setText(ClientItem.getLeCodeCl());
        holder.RsNomView.setText( ClientItem.getLeRsNom());
        holder.VilleView.setText(ClientItem.getLaVille());
        holder.TelView.setText(ClientItem.getLeTel());

        return convertView;
    }

     // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        Client_Pers_list.clear();
        if (charText.length() == 0) {
            Client_Pers_list.addAll(Client_Pers_arraylist);
        } else {
            for (Client_Pers wp : Client_Pers_arraylist) {
                if (wp.getLeRsNom().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    Client_Pers_list.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }

    static class ViewHolder {
        TextView CodeClView;
        TextView RsNomView;
        TextView VilleView;
        TextView TelView;
    }

}

当我只使用arraylist而不是arraylist和List时,我应该把它放在if

之下
 public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        Client_Pers_arraylist.clear();
        if (charText.length() == 0) {
            ??????????????
        } else {
            for (Client_Pers wp : Client_Pers_arraylist) {
                if (wp.getLeRsNom().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    Client_Pers_arraylist.add(wp);                  
                }
            }
        }
        notifyDataSetChanged();
    }

1 个答案:

答案 0 :(得分:0)

在getView中,您使用Client_Pers_arraylistClient_Pers_list

我想知道为什么你们都在适配器类中使用List和ArrayList。仅使用ArrayList。