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 类。有人可以帮我..谢谢。

4

1 回答 1

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);
    }
}
于 2013-02-10T08:26:32.413 回答