1

ListViewから拡張されたカスタムアダプターを使用して実装しましたArrayAdapter。私の問題はListView、読み込みが遅い場合があることです。つまり、空白のアクティビティが最初に読み込まれListView、次に がListView出てきます。最悪の場合、「強制終了または待機」するよう求められます。ユーザーにとって煩わしいので、読み込みの遅さを改善したいと思います。

ただし、ロードが高速で、ほぼ即時に完了する場合もあります。

しかし、私のListViewデザインが正しいことを確認したいのですが、読み込みが遅いという問題がデザインにないことを確認したいと思います。この議論が、私と同じ問題に直面している他の人々に役立つように.

私の ListView は次のように設計されています。

各 ListItem には、添付の図に示すように、サムネイル画像、ID テキスト、および矢印画像の 3 つのコンポーネントがあります画像

のロード プロセスではListView、(1) すべての ID テキストがデータベースから取得され、ListArray に入力されます。List<String> listIDs

        public class MyListFragment extends ListFragment implements OnItemClickListener {
                dbHelper = new TrackerDBAdapter(getActivity());
                dbHelpLatLong = new LatLogDBAdapter(getActivity());
                dbHelpNotification = new NotificationDatabaseAdapter(getActivity());
                dbHelper.open();
                Cursor cursor = dbHelper.fetchAllTrackerInTheList();
                listIDs = new ArrayList<String>();
                activationStatus = new ArrayList<String>();
                thisListFragmentContext = getActivity();
                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                     listIDs.add(cursor.getString(1));                   
                }
            dbHelper.close();

(2)Then my custom list adapter is called. 




        adapter = new customList_Adaptor(thisListFragmentContext,
                        R.layout.list_row, listIDs, this);
        }

That is the loading process inside my `ListFragment`.

(3) 次のクラスは私のカスタムで、 を使用してArrayAdapterサムネイルをロードするように実装しました。私のクエリはImageViewAsyncTask

(i) データベースから ID テキストを取得し、矢印イメージをロードしています。それらのプロセスも入れるべきAsyncTaskですか?(ii)必要な場合、別のものを実装するか、サムネイル画像の読み込みにAsyncTask使用するものと同じものを使用する必要がありますか? AsyncTask(iii)これらの中で、ロードが遅いために疑わしいプログラム設計のどの側面を改善することができますか?

public class customList_Adaptor extends ArrayAdapter<String>{


    protected static final int CAMERA_REQUEST = 0;
    private TrackerDBAdapter dbHelper;
    private Context context;
    private List<String> listIDs = new ArrayList<String>();
    private List<String> activationState = new ArrayList<String>();
    public MyListFragment mMyListFragment;
    public customList_Adaptor(Context context, int textViewResourceId,
            List<String> objects, List<String> activationStatus, MyListFragment mMyListFragment) {
        super(context, textViewResourceId, objects);
        this.setContext(context);
        this.listIDs = objects;
        this.activationState = activationStatus;
        this.mMyListFragment= mMyListFragment;
        dbHelper = new TrackerDBAdapter(context);
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(listIDs != null)
            return listIDs.size();
        else
            return 0;
    }

    @Override
    public String getItem(int arg0) {
        // TODO Auto-generated method stub
        if(listIDs != null)
            return listIDs.get(arg0);
        else
            return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder viewHolder=new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(vi==null){
            vi = inflater.inflate(R.layout.list_row, parent, false);
            viewHolder.id=(TextView)vi.findViewById(R.id.title);
            viewHolder.thumbnailImage=(ImageView)vi.findViewById(R.id.list_image);
            viewHolder.activationStatus = (TextView)vi.findViewById(R.id.activated);
            //lazy load image
            BitmapWorkerTask task = new BitmapWorkerTask(viewHolder.thumbnailImage);
            task.execute(position);

            viewHolder.arrow=(ImageView)vi.findViewById(R.id.list_arrow);
            vi.setTag(viewHolder);
        }
        else
            viewHolder=(ViewHolder) vi.getTag();

        viewHolder.thumbnailImage.setOnClickListener(new onMyClick(position));               
        // Setting all values in listview
        viewHolder.id.setText(listIDs.get(position));   

        if(activationState.get(position).equals("Not activated yet")){
             viewHolder.activationStatus.setText(activationState.get(position));
             viewHolder.activationStatus.setTextColor(android.graphics.Color.GRAY);
        }
        else if(activationState.get(position).equals("Activated"))
            viewHolder.activationStatus.setText("");
        return vi;
    }

    public class onMyClick implements OnClickListener {

        private final int pos;
        public onMyClick(int pos) {
            this.pos = pos;
        }

        @Override
        public void onClick(View v) {
            MyListFragment.clickedimageView = (ImageView) v.findViewById(R.id.list_image);                      
            mMyListFragment.imagepos(pos);
        }

    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    //Lazy image update
    class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;
        private int data = 0;

        public BitmapWorkerTask(ImageView imageView) {
            // Use a WeakReference to ensure the ImageView can be garbage collected
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        // Decode image in background.
        @Override
        protected Bitmap doInBackground(Integer... params) {
            setData(params[0]);
            Bitmap bitmap = null;
            dbHelper.open();
            Cursor mCursor = dbHelper.getImagebyIDnumber(getData());
            byte[] img_bytes = mCursor.getBlob(13);
            bitmap = BitmapFactory.decodeByteArray(img_bytes, 0, img_bytes.length);         

            dbHelper.close();        
            return bitmap;
        }

        // Once complete, see if ImageView is still around and set bitmap.
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (imageViewReference != null && bitmap != null) {
                final ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }
    }

   }


public class ViewHolder {
      TextView id;
      TextView activationStatus;
      ImageView thumbnailImage;
      ImageView arrow;
}
4

1 に答える 1