数据库搜索仅返回1个搜索结果

时间:2015-03-13 18:15:17

标签: android sql sqlite search android-studio

我制作了一个电影数据库应用程序,用户可以在其中搜索电影,然后在scrollView中显示搜索结果。然而,每当我搜索一个应该带回多个像“the”这样的电影的字符串时,它只会返回它看到的第一个结果。知道为什么吗?

MainActivity:

public void search(View v){
    EditText search = (EditText)findViewById(R.id.edtSearch);
    String searchresult = "%" + search.getText().toString() + "%";
    db = new DbHelper(this).getReadableDatabase();
    String[] column = {"*"};
    String where = "film LIKE ? OR actor LIKE ? OR actor2 LIKE ? OR director LIKE ?";
    String[] selArgs = {searchresult, searchresult, searchresult, searchresult};
    Cursor results = db.query("FILMTABLE", column, where, selArgs, null, null, null);
    film(results);
}

public void film (Cursor c) {

        c.moveToFirst();
        int filmint = c.getColumnIndex("film");
        int id = c.getColumnIndex("id");
        String film = c.getString(filmint);
        int filmID = c.getInt(id);

        TextView txt = new TextView(getApplicationContext());
        txt.setId(filmID);
        txt.setText(film);
        txt.setTextColor(Color.BLACK);
        txt.setTextSize(16);
        txt.setClickable(true);
        txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MainActivity4.class);
                intent.putExtra(FILM_ID_KEY, String.valueOf(v.getId()));
                startActivity(intent);
            }
        });

        ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
        scrollView.addView(txt);

    }

1 个答案:

答案 0 :(得分:3)

您需要使用游标迭代结果。

if (c.moveToFirst())
    {
        do {
            // Get values here
        } while (c.moveToNext());
    }
c.close();