我有一个列表视图,显示500多个单词及其含义。 问题是,当我加载listview时,它真的需要很多时间.. 我想添加一个监听器,如OnScrollListener,这样当用户加载listview时,它只会加载100个单词,然后当用户向下滚动它时会添加更多单词。 我使用SimpleCursorAdapter我的代码是这样的:
在onCreate()方法中:
dbHelper = new WordDbAdapter(this);
dbHelper.open();
//Clean all data
dbHelper.deleteAllWords();
//Add some data
dbHelper.insertSomeWords();
//Generate ListView from SQLite Database
displayListView();
在onCreate()方法之外:
@SuppressWarnings("deprecation")
private void displayListView() {
Cursor cursor = dbHelper.fetchAllWords();
// The desired columns to be bound
String[] columns = new String[] {
WordDbAdapter.KEY_WORD,
WordDbAdapter.KEY_ROWID,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.Word,
R.id.imgStar,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.word_info,
cursor,
columns,
to
);
ListView listView = (ListView) findViewById(R.id.Diclist);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the word name from this row in the database.
String wordSelected =
cursor.getString(cursor.getColumnIndexOrThrow("word"));
String wordMeaning = cursor.getString(cursor.getColumnIndexOrThrow("meaning"));
String wordSpeak =
cursor.getString(cursor.getColumnIndexOrThrow("speakword"));
EditText TextDic = (EditText) findViewById(R.id.TextDic);
TextDic.setText(wordSelected);
speakMeaning = wordMeaning;
speakWord = wordSpeak;
}
});
EditText TextDic = (EditText) findViewById(R.id.TextDic);
TextDic.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
speakWord = "";
speakMeaning = "";
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
dataAdapter.getFilter().filter(s.toString());
}
});
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.fetchWordsByWord(constraint.toString());
}
});
}
我还有另外两个DBAdapter和Word类。 有人可以帮助我..谢谢。
答案 0 :(得分:0)
public class MyListModel extends AbstractListModel <String>
{
public String [] items = new String [1000];
@Override
public int getSize ()
{
return items.length;
}
@Override
public String getElementAt (int index)
{
if (items [index] == null)
loadRange (Math.max (0, index - 50), Math.min (1000, index + 50));
return items [index];
}
private void loadRange (int begin, int end)
{
// Load items from begin (inclusive) to end (exclusive) from database.
for (int i = begin; i < end; i++)
{
if (items [i] == null)
items [i] = "Item #" + i + ", loaded at " + new Date ();
}
}
public static void main (String [] args)
{
JList <String> list = new JList <String> (new MyListModel ());
list.setPrototypeCellValue (list.getModel ().getElementAt (999));
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane ().setLayout (new BorderLayout ());
frame.getContentPane ().add (
new JScrollPane (
list,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
BorderLayout.CENTER);
frame.pack ();
frame.setVisible (true);
}
}