当EditText为空时清空ListView

时间:2013-08-13 08:47:23

标签: android sqlite listview android-edittext

我有代码从SQLite游标获取数据并放入listview。

private void displayListView() {
    cursor = myDatabase.getJoinedInfo(etSearch.getText().toString().trim());

    String[] columns = new String[] { "re_value", "g_value", "ke_value" };

    int[] to = new int[] { R.id.tvHiragana, R.id.tvMeaning, R.id.tvKanji };

    dataAdapter = new SimpleCursorAdapter(this, R.layout.wordonlist,
            cursor, columns, to, 0);

    ListView listView = (ListView) findViewById(R.id.lvWordlist);
    // Assign adapter to ListView

    listView.setAdapter(dataAdapter);
}

当EditText为空时,前10行。当EditText为空时如何显示空的ListView?

3 个答案:

答案 0 :(得分:1)

做这样的事情:

If(yourEditText.length() == 0)
    yourListView.setAdapter(null);

或者您可以使用:

If(yourEditText.length() == 0)
    yourAdapter.clear();

答案 1 :(得分:0)

在EditText为空之前不要设置适配器。您可以在EditText中有一些数据后设置适配器。

答案 2 :(得分:0)

试试这个

private void displayListView() {
    if (etSearch.getText().toString().trim().equals("")) {
        listView.setAdapter(null);
    } else {
        cursor = myDatabase.getJoinedInfo(etSearch.getText().toString()
                .trim());

        String[] columns = new String[] { "re_value", "g_value", "ke_value" };

        int[] to = new int[] { R.id.tvHiragana, R.id.tvMeaning,
                R.id.tvKanji };

        dataAdapter = new SimpleCursorAdapter(this, R.layout.wordonlist,
                cursor, columns, to, 0);

        ListView listView = (ListView) findViewById(R.id.lvWordlist);
        // Assign adapter to ListView

        listView.setAdapter(dataAdapter);
    }
}