ListView中的字母索引与SimpleCursorAdapter非常慢

时间:2016-09-01 14:52:59

标签: android listview simplecursoradapter

我正在尝试使用SimpleCursorAdapter在ListView中实现Alphabet-Index。 它正在工作,但它非常慢,当我试图上下滚动列表时,手机正在被击中。我认为它正在发生,因为我的编码方式由于缺乏知识。所以我需要知道这个代码的错误或错误是什么。如果除此之外还有其他方式,请告诉我。

这是具有上述问题的片段代码集。

{
  "name": "doors_right",
  "rotation_point": {
    "px": 49,
    "py": 0,
    "pz": 26,
    "onclick": [{
      "rx": 0,
      "ry": 0,
      "rz": 0
    }, {
      "rx": 0,
      "ry": Math.PI / 2,
      "rz": 0
    }]
  },
  "elements": [{
    "e_type": "box",
    "h": 100,
    "w": 50,
    "d": 2,
    "x": -24,
    "y": 0,
    "z": 0,
    "rx": 0,
    "ry": 0,
    "rz": 0,
    "textures": ["textures/drewno.jpg", "textures/drewno.jpg", "textures/drewno.jpg", "textures/drewno.jpg", "textures/drewno.jpg", "textures/drewno.jpg"]
  }]
}

fragment_en.xml(上述片段的布局)

public class EnFragment extends Fragment {

    private TestAdapter dbHelper;
    private myCursorAdapter dataAdapter;

    public EnFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        dbHelper = new TestAdapter(getContext());
        dbHelper.createDatabase();
        dbHelper.open();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_en, container, false);
        setListView(view);
        return view;
    }

    private void setListView(View view) {
        Cursor cursor = dbHelper.getAllWord("enDic");

        String[] columns = new String[]{
                "word",
                "definition",
                "_id",
                "favourite"
        };

        int[] to = new int[]{
                R.id.txt_word,
                R.id.txt_def,
                R.id.txt_id
        };

        dataAdapter = new myCursorAdapter(
                getContext(), en_word_row,
                cursor,
                columns,
                to,
                0);

        Button btnSubmitEng = (Button) view.findViewById(R.id.btnSubmitEng);

        final ListView listView = (ListView) view.findViewById(R.id.listView);
        listView.setAdapter(dataAdapter);
        listView.setEmptyView(btnSubmitEng);
        listView.setFastScrollEnabled(true);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);
                final String word = cursor.getString(cursor.getColumnIndexOrThrow("word"));
                Toast.makeText(getContext(), word, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void textSearch(String searchTxt){
        String newStr = searchTxt.replace(" ","");
        dataAdapter.getFilter().filter(newStr);

        dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                return dbHelper.getWordByName("enDic",constraint.toString());
            }
        });
    }

    private class myCursorAdapter extends SimpleCursorAdapter implements SectionIndexer {

        private final AlphabetIndexer mAlphabetIndexer;

        public myCursorAdapter(Context context, int layout, 
                               Cursor c, String[] from, int[] to, int flags) {
            super(context, layout, c, from, to, flags);
            mAlphabetIndexer = new AlphabetIndexer(c, 1, "අBCDEFGHIJKLMNOPQRSTUVWXYZ");
        }

        public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return inflater.inflate(en_word_row, parent, false);
        }

        @Override
        public void bindView(View view, Context context, final Cursor cursor) {
            super.bindView(view, context, cursor);

            final ImageButton imgFav = (ImageButton) view.findViewById(R.id.img_fav);
            final String _id = cursor.getString(cursor.getColumnIndex("_id"));
            final Integer fav = Integer
                    .valueOf(cursor.getString(cursor.getColumnIndex("favourite")));

            if (fav == 1)
                imgFav.setImageResource(R.drawable.fav_on);
            else
                imgFav.setImageResource(R.drawable.fav_off);

            imgFav.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dbHelper.isFavourite("enDic", _id)){
                        imgFav.setImageResource(R.drawable.fav_off);
                        dbHelper.favUpdate("enDic", _id,0);
                    }
                    else{
                        imgFav.setImageResource(R.drawable.fav_on);
                        dbHelper.favUpdate("enDic", _id,1);
                    }
                }
            });
        }

        @Override
        public Object[] getSections() {
            return mAlphabetIndexer.getSections();
        }

        @Override
        public int getPositionForSection(int sectionIndex) {
            return mAlphabetIndexer.getPositionForSection(sectionIndex);
        }

        @Override
        public int getSectionForPosition(int position) {
            return mAlphabetIndexer.getSectionForPosition(position);
        }
    }
}

en_word_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sldroid.mecdic_v21.fragment.EnFragment"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/transparent"
        android:listSelector="@android:color/transparent"
        android:scrollbarStyle="outsideOverlay"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit new word"
        android:layout_marginTop="55dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:id="@+id/btnSubmitEng"/>

</LinearLayout>

TestAdapter的方法

<LinearLayout
    android:id="@+id/cardView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:descendantFocusability="blocksDescendants">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:paddingLeft="20dp"
        android:paddingRight="20dp">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="3"
            android:paddingTop="10dp"
            android:paddingBottom="10dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="25dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/txt_word"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Word"
                    android:textSize="18dp"
                    android:textColor="#424242"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/txt_def"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="definition"
                    android:textSize="10dp"
                    android:paddingTop="5dp"
                    android:paddingLeft="10dp"
                    android:textColor="#FF757575"
                    android:lines="1"/>

            </LinearLayout>

            <TextView
                android:id="@+id/txt_id"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:visibility="invisible"/>

        </LinearLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="0.5">

            <ImageButton
                android:id="@+id/img_fav"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/fav_off"
                android:scaleType="centerInside"
                android:layout_gravity="center"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                style="@style/Base.Widget.AppCompat.Button.Borderless"/>

        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

应该在doInBackground方法中调用GetAllWord和getWordByName,并在postExecute中更新你的适配器。

private class GetAllWord extends AsyncTask<Object, Object, Cursor> {

    @Override
    protected Cursor doInBackground(Object... params) {
        return GetAllWord("your text");
    }

    @Override
    protected void onPostExecute(Cursor result) {
        if (result != null) {
            //update the adapter with the result
        }
    }
}