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());
         }
     });
  }

DBAdapterとWord用に他に2つのクラスもありました。誰かが私を助けることができます..ありがとう。

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 に答える