0

我有从 SQLite 游标中获取数据并放入列表视图的代码。

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?

4

3 回答 3

1

做这样的事情:

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

或者您可以使用:

If(yourEditText.length() == 0)
    yourAdapter.clear();
于 2013-08-13T08:55:38.170 回答
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);
    }
}
于 2013-08-13T08:57:05.900 回答
0

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

于 2013-08-13T08:51:05.233 回答