【问题标题】:Textwatcher in custom listview with SQLite database带有 SQLite 数据库的自定义列表视图中的 Textwatcher
【发布时间】:2014-07-12 16:09:19
【问题描述】:

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

我在 MainActivity.java 中添加了一个列表视图适配器

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

 }

【问题讨论】:

    标签: java android sqlite listview textwatcher


    【解决方案1】:

    您的意思是要过滤列表视图中的项目。如果是这样,您可以使用 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.
    }
    

    【讨论】:

    • 那是什么?这是简单的逻辑。请不要那样做。这对我来说是个严重的问题。如果你有任何例子,请告诉我。
    • 很抱歉,我可能误解了您的问题。您的意思是要过滤列表视图中的结果以显示?
    • 是的,但是这些值来自 SQLite 数据库,所以实际上过滤器应该是数据库。但我不知道。
    • 我对SQL过滤不太熟悉。如果可以在 listview 上过滤结果,您可以尝试过滤 listview 适配器的接口。 link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    相关资源
    最近更新 更多