使用SQLite数据库的自定义列表视图中的Textwatcher

时间:2014-07-12 16:09:19

标签: java android sqlite listview textwatcher

我想在自定义列表视图中添加一个过滤器,它从sqlite数据库中获取值。是否可以根据textview的两个值进行过滤?请帮我添加一个过滤查询。如果您有任何文本观察者示例,请告诉我。

我在MainActivity.java中添加了一个listview适配器

   lv = (ListView) findViewById(R.id.listView1);
    mydb = new DBHelper(this);
    Log.d("tag", mydb.listfromdb().toString());
    lv.setAdapter(new ViewAdapter(mydb.listfromdb()));

public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_item, null);
        }
        Listcollection o = collectionlist.get(position);

        if (o != null) {
            TextView idText = (TextView) convertView
                    .findViewById(R.id.lvid);
            TextView nameText = (TextView) convertView
                    .findViewById(R.id.lvname);

            TextView dateText = (TextView) convertView
                    .findViewById(R.id.lvdate);
            TextView phoneText = (TextView) convertView
                    .findViewById(R.id.lvjno);

            if (idText != null) {
                idText.setText(Integer.toString(o.getId()));
            }
            if (nameText != null) {
                nameText.setText("Name : "
                        + collectionlist.get(position).getName());
            }
            if (dateText != null) {
                dateText.setText("Date of Complain : "
                        + collectionlist.get(position).getCdate());
            }
            if (phoneText != null) {
                phoneText.setText("Job No.: "
                        + collectionlist.get(position).getType());
            }
        }

        return convertView;
    }

这里我从DBHelper.java

获取值
public ArrayList<Listcollection> listfromdb() {
    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<Listcollection> results = new ArrayList<Listcollection>();

    Cursor crs = db.rawQuery("select * from contacts", null);
    while (crs.moveToNext()) {
        Listcollection item = new Listcollection();
        item.setId(crs.getInt(crs.getColumnIndex(CONTACTS_COLUMN_ID)));
        item.setName(crs.getString(crs.getColumnIndex(C_NAME)));
        item.setCdate(crs.getString(crs.getColumnIndex(C_CDATE)));
       item.setType(crs.getString(crs.getColumnIndex(C_TYPE)));
        results.add(item);
    }

    db.close();
    return results;
}

我删除了一些方法后也包含了DBHelper.java。

public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String C_NAME = "name";
public static final String C_TYPE = "type";
public static final String C_ADDRESS = "address";
public static final String C_DATE = "sdate";
public static final String C_PHONE = "phone";
public static final String C_PNAME = "pname";
public static final String C_MNO = "mno";
public static final String C_CDATE = "cdate";
public static final String C_DNAME = "dname";
public static final String C_DPHONE = "dphone";
public static final String C_TSCHARGE = "tscharge";
public static final String C_DAMOUNT = "damount";
public static final String C_FANDSOL = "fandsol";
public static final String C_TRNO = "trno";
public static final String C_TCRAMOUNT = "tcramount";


private HashMap hp;

public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

public ArrayList<Listcollection> listfromdb() {
    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<Listcollection> results = new ArrayList<Listcollection>();

    Cursor crs = db.rawQuery("select * from contacts", null);
    while (crs.moveToNext()) {
        Listcollection item = new Listcollection();
        item.setId(crs.getInt(crs.getColumnIndex(CONTACTS_COLUMN_ID)));
        item.setName(crs.getString(crs.getColumnIndex(C_NAME)));
        item.setCdate(crs.getString(crs.getColumnIndex(C_CDATE)));
       item.setType(crs.getString(crs.getColumnIndex(C_TYPE)));
        results.add(item);
    }

    db.close();
    return results;
}

 }

1 个答案:

答案 0 :(得分:0)

您的意思是要过滤listview中的项目。如果是这样,你可以使用if语句来显示正确的值。

String string = x;   // where x is the string that you want to display
if(input == string)    {
    // set the field in listview to the input.
}