Listview非常缓慢地过滤数据库中包含的数据

时间:2014-08-18 07:07:43

标签: android database listview dictionary filter

我正在使用英语到波斯语词典...... 不幸的是,我无法使用Lazzy Listview示例在我的项目中, 我的项目最初有一个列表,filter出了大约5万个来自数据库的单词,所以发生了慢速输入和更新列表,请帮助我使用Lazzy Listview在我的项目中使用优化过滤器来减少使用内存和提高速度.... TNX

这是我的课程:

MainActivity

public class MainActivity extends Activity {

ListView                         list;
ImageView                        clearText;
EditText                         editsearch;
Typeface                         edtSearchFont;
static String[]                  word;
static int[]                     index;
static ListViewAdapter           adapter;
static ArrayList<ListViewStruct> arrayStructList = new ArrayList<ListViewStruct>();
Context                          context;


// onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_main);
    context = this;

    Cursor en = G.database.rawQuery("SELECT * FROM English", null);
    word = new String[en.getCount()];
    index = new int[en.getCount()];
    int i = 0;
    ListViewStruct wp = null;
    if (en.moveToFirst())
    {
        do
        {
            try {
                word[i] = en.getString(en.getColumnIndex("English_Word"));
                index[i] = en.getInt(en.getColumnIndex("Word_ID"));
                wp = new ListViewStruct(word[i], index[i]);
                arrayStructList.add(wp);
                i++;

            }
            catch (Exception e) {

                e.printStackTrace();
            }
        }
        while (en.moveToNext());
    }
    en.close();

    list = (ListView) findViewById(R.id.listview);
    adapter = new ListViewAdapter(this, arrayStructList);
    list.setAdapter(adapter);
    editsearch = (EditText) findViewById(R.id.search);
    edtSearchFont = Typeface.createFromAsset(context.getAssets(), "fonts/listFont.ttf");
    editsearch.setTypeface(edtSearchFont);
    editsearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }


        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                                      int arg2, int arg3) {
            // TODO Auto-generated method stub
        }


        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }
    });

    clearText = (ImageView) findViewById(R.id.clearTextImage);

    clearText.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            editsearch.setText("");

        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //getMenuInflater().inflate(R.menu.activity_main, menu);
    menu.add(0, 1, 1, "About");
    menu.add(0, 2, 2, "Exit");

    return true;

}


//@SuppressWarnings("deprecation")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    switch (item.getItemId()) {

        case 1: {

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("About")
                    .setMessage("\n" + "© Copyright" + "\n" + "Hossein Tahmasebi , 2014" + "\n")
                    .setCancelable(false)
                    .setIcon(R.drawable.logo)
                    .setNegativeButton("Ok", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();

            break;

        }
        case 2: {

            this.finish();

        }

    }

    return super.onOptionsItemSelected(item);
};

}

ListViewAdapter

public class ListViewAdapter extends BaseAdapter {

// Declare Variables
Context                           mContext;
LayoutInflater                    inflater;
private List<ListViewStruct>      localWordsList;
private ArrayList<ListViewStruct> arraystructlist;
public int                        index;
Typeface                          listFont;


public ListViewAdapter(Context context, List<ListViewStruct> mainWordList) {
    mContext = context;
    this.localWordsList = mainWordList;
    inflater = LayoutInflater.from(mContext);
    this.arraystructlist = new ArrayList<ListViewStruct>();
    this.arraystructlist.addAll(mainWordList);

}


public class ViewHolder {

    TextView word;

}


@Override
public int getCount() {
    return localWordsList.size();
}


@Override
public ListViewStruct getItem(int position) {
    return localWordsList.get(position);
}


@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(final int position, View view, ViewGroup parent) {

    final ViewHolder holder;

    listFont = Typeface.createFromAsset(mContext.getAssets(), "fonts/listFont.ttf");

    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.listview_item, null);
        holder.word = (TextView) view.findViewById(R.id.word);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    holder.word.setText(localWordsList.get(position).getWord());
    holder.word.setTypeface(listFont);

    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(mContext, enTranslatedResultActivity.class);
            intent.putExtra("index", (localWordsList.get(position).getIndex()));
            intent.putExtra("word", (localWordsList.get(position).getWord()));
            mContext.startActivity(intent);
        }
    });

    return view;
}


// Filter Class
public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    localWordsList.clear();
    if (charText.length() == 0) {
        localWordsList.addAll(arraystructlist);
    }
    else
    {
        for (ListViewStruct wp: arraystructlist)
        {
            if (wp.getWord().toLowerCase(Locale.getDefault()).startsWith(charText))
            {
                localWordsList.add(wp);
            }
        }
    }
    notifyDataSetChanged();

}

}

ListViewStruct

public class ListViewStruct {
    private int    index;
    private String word;
    private String translatedWord;


    public ListViewStruct(String word, int index) {

        this.word = word;
        this.index = index;

    }


    public int getIndex() {
        return this.index;
    }


    public String getWord() {
        return this.word;
    }


    public String getPopulation() {
        return this.translatedWord;
    }
}

G classHelperIOenTranslatedResultActivity以显示已翻译的结果词.....

如果要求这些课程在这里,请告诉我把它们放进去,

感谢您的关注.....

1 个答案:

答案 0 :(得分:0)

这是cursorAdapter(使用它而不是baseAdapter)的示例,您需要将光标传递给它,而不需要将光标放在while循环中的arraylist中

public class ImageCursorAdapter extends CursorAdapter {
public ImageCursorAdapter(Context context, Cursor c) {
    super(context, c);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView word = (TextView) view.findViewById(R.id.word);
    word.setText(cursor.getString(cursor.getColumnIndex("English_Word")));
    word.setTypeface(listFont);
view.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        Intent intent = new Intent(context, enTranslatedResultActivity.class);
        intent.putExtra("index", (cursor.getInt(cursor.getColumnIndex("Word_ID")));
        intent.putExtra("word", (cursor.getColumnIndex("English_Word")));
        context.startActivity(intent);
        }
  });
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View convertView = infalInflater.inflate(R.layout.listview_item, null);
    bindView(convertView , context, cursor);
    return convertView ;
}
}  
在newView中

执行类似于getView方法的操作(不使用viewHolder)
而在bindView中只需要为您的视图设置数据 您还可以设置editText并将其与此适配器绑定,以便为editText设置OntextchangeListener,然后在用户输入editText时快速搜索。
可能更好的方式可以获得更好的表现,但我分享了我所知道的一切