1
我已经制作了电影数据库应用程序,用户可以在其中搜索电影,然后在scrollView中显示搜索结果。然而,无论何时我搜索一个应该带回像“the”这样的多个电影的字符串,它只会返回它看到的第一个结果。任何想法为什么?数据库搜索只返回1搜索结果
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);
}
您只能获取一个数据...因为您只从光标获取一个数据。你需要通过cursor.moveToNext()循环来获取剩余的光标dat(如果有)。请记住最后关闭游标。 – khubaib 2015-03-13 18:59:12