我想使用edittext从数据库中搜索名称

时间:2015-02-20 12:19:31

标签: android android-edittext

public class SearchActivity extends Activity {

    private EditText Search_By_Name;
    private ArrayList<Personal_loan> array;
    private ArrayAdapter<Personal_loan> adapter;
    private ListView listView;
    SQLiteDatabase db;
    Cursor cursor ;
    private DBHelper dbHelper;
    Personal_loan p = new Personal_loan();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Search_By_Name = (EditText)findViewById(R.id.SearchName);
        array = new ArrayList<Personal_loan>();
        listView = (ListView) findViewById(R.id.listView1);
        adapter = new ArrayAdapter<Personal_loan>(this, android.R.layout.simple_list_item_1, array);
        listView.setAdapter(adapter);
        getData();
    }
    private void getData() {
        DBHelper helper = new DBHelper(this);
        db = helper.getReadableDatabase();
        String columns[] = new String[] {ID,Name,START_DATE,END_DATE,LOAN_AMOUNT,RATE_OF_INTEREST,FINAL_AMOUNT};
        cursor = db.query( false, LOAN_TABLE, columns, null, null, null, null, null, null, null);
        if (!cursor.isAfterLast()) {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                p = new Personal_loan();
                p.id = cursor.getInt(0);
                p.name = cursor.getString(1);
                p.sd = cursor.getString(2);
                p.ed = cursor.getString(3);
                p.la = cursor.getDouble(4);
                p.roi = cursor.getFloat(5);
                p.amt=cursor.getString(6);
                array.add(p);
                cursor.moveToNext();
                Search_By_Name.addTextChangedListener(new TextWatcher() {
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        // TODO Auto-generated method stub

                    } 
                    public void beforeTextChanged(CharSequence s, int start, int count,
                            int after) {
                        // TODO Auto-generated method stub
                    }
                    public void afterTextChanged(Editable s) {
                        // TODO Auto-generated method stub
                        s=Search_By_Name.getText();
                        p.name=s.toString();
                        adapter.getFilter().filter(s.toString());
                    }
                });
            }
            cursor.close();
            db.close();
            adapter.notifyDataSetChanged();
        }       
    }
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        Intent i=new Intent(SearchActivity.this,FirstActivity.class);
        startActivity(i);
    }
}

这是我的更新代码,它过滤了数据,但是使用了最后一个字段,而不是名字字段...它取了最后一个字段进行过滤,如果有人知道答案那么分享

1 个答案:

答案 0 :(得分:0)

试试这个......

  Search_By_Name .addTextChangedListener(new TextWatcher() {

          @Override public void afterTextChanged(Editable arg0) { 
          // TODO  Auto-generated method stub 
         String text =  search.getText().toString().toLowerCase(Locale.getDefault()); //
          Your_Activity.this.adapter.filter(text); }

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

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

参考link