2013-02-10 93 views
0

我有一个列表视图,它显示500多个单词及其含义。 问题是,当我加载列表视图它真的需要很多时间.. 我想添加一个监听器,如OnScrollListener,以便当用户加载列表视图时,它将只加载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()); 
     } 
    }); 
    } 

我还有2个DBAdapter和Word的其他类。 有人可以帮助我..谢谢。

+0

这应该有助于http://stackoverflow.com/questions/6573489/android-endless-scrolling-listview-and-cursor – 2013-02-10 08:37:49

+0

感谢您的链接M-WaJeEh ... :) – Jin 2013-02-12 01:05:43

回答

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); 
    } 
} 
+0

的问题是相关的到Android。 – 2013-02-10 08:45:18

相关问题