我正在创建一个有6个赛季的sqlite数据库的android书,但listview需要几秒钟才能加载数据。 我知道我实施onscroll方法来实现这一点,但我不知道如何管理它 我的代码是:
public class list_story extends ListActivity{
private database db;
private String[] Name;
private String sea;
Context c;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list_story);
db = new database(this);
Bundle ex = getIntent().getExtras();
sea = ex.getString("sea");
refresh();
setListAdapter(new AA());
}
class AA extends ArrayAdapter<String>{
public AA(){
super(list_story.this,R.layout.row_list,Name);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater in = getLayoutInflater();
View row = in.inflate(R.layout.row_list, parent,false);
txt.setText(Name[position]);
return (row);
}
}
private void refresh(){
db.open();
int s =db.Story_count("content", sea);
Name = new String[s];
Fav=new String[s];
Tasvir=new String[s];
Scientific=new String[s];
English=new String[s];
for (int i = 0; i < s; i++) {
Name[i]=db.Story_display(i, sea, "content", 1);
Fav[i]=db.Story_display(i, sea, "content", 4);
Tasvir[i]=db.Story_display(i, sea, "content", 5);
Scientific[i]=db.Story_display(i, sea, "content", 6);
English[i]=db.Story_display(i, sea, "content", 7);
}
db.close();
} }
答案 0 :(得分:0)
基本上每当您尝试给新视图充气时,您都会真正夸大新视图。问题在于,如果您有100多个项目可以放入列表视图,那么您已经定义了100多个视图。这非常低效。使用ViewView的ViewHolder模式,或使用RecyclerView。更好的是使用RecyclerView。网上有很多关于这两者的信息。只需查看&#34;如何在列表视图中使用ViewHolder&#34;
答案 1 :(得分:0)
如果您只使用单个文本字段和db,我会建议您使用simpleCursorAdapter:
A sub-pattern begins with a ?, *, +, @, or ! character followed by a pattern-list
enclosed in parentheses. Pattern-lists themselves can contain sub-patterns.
The following list describes valid sub-patterns.
?(pattern-list)
Matches exactly zero or exactly one occurrence of the specified pattern-list.
*(pattern-list)
Matches zero or more occurrences of the specified pattern-list.
+(pattern-list)
Matches one or more occurrences of the specified pattern-list.
@(pattern-list)
Matches exactly one occurrence of the specified pattern-list.
!(pattern-list)
Matches any string that does not match the specified pattern-list.
如果您还有其他问题,请查看这个很棒的示例https://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/