Update BaseAdapter in backgrond Thread, to insert Gallery items in run-time.

Thảo luận trong 'Điện thoại, viễn thông' bắt đầu bởi bka15, 30/9/12.

  1. bka15 New Member

    bka15

    Tham gia ngày:
    18/7/12
    Bài viết:
    128
    Đã được thích:
    105
    Điểm thành tích:
    0
    Refer to the last post "Implement a photo bar using android.widget.Gallery, with custom BaseAdapter, and custom object"; in some case such as the Gallery items have to be downloaded from internet, we cannot wait too long to load Gallery in UI thread.



    [​IMG]

    We will modify the Java code in "Implement a photo bar using android.widget.Gallery, with custom BaseAdapter, and custom object", to implement a background Thread to insert Gallery items in a dummy delay of 500ms for each item, in run-time. After each item inserted, send a message from the thread to the handle in UI thread to call notifyDataSetChanged() to update the Gallery.


    PHP:
    package com.AndroidPhotoBar;
     
    import java.util.ArrayList;
     
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
     
    public class 
    AndroidPhotoBarActivity extends Activity {
      
     
    MyThread myThread;
      
     
    ArrayList<ImageItemarrayImageItem;
     
    MyAdapter myAdapter;
      
       public class 
    ImageItem {
         
        
    Bitmap bitmapImage;
         
        
    ImageItem(){
         
    //To simplify, we use a default image here
         
    bitmapImage BitmapFactory.decodeResource(
           
    AndroidPhotoBarActivity.this.getResources(), R.drawable.icon);
        }
         
        public 
    Bitmap getImage(){
         return 
    bitmapImage;
        }
         
     }
       
       
    Gallery myPhotoBar;
     
     
    /** Called when the activity is first created. */
       
    @Override
       
    public void onCreate(Bundle savedInstanceState) {
           
    super.onCreate(savedInstanceState);
           
    setContentView(R.layout.main);
           
    myPhotoBar = (Gallery)findViewById(R.id.photobar);
           
           
    myAdapter = new MyAdapter(this); //init myAdapter without ImageItem
           
    myPhotoBar.setAdapter(myAdapter);
           
           
    myThread = new MyThread();
           
    myThread.setRunning(true);
           
    myThread.start();
     
       }
       
       private 
    void insertDummyImageItem(int cnt){
     
        
    //Insert dummy ImageItem into myAdapter
        
    for(int i 0cnti++){
         
    myAdapter.addImageItem(new ImageItem());
        }
     
       }
     
       public class 
    MyAdapter extends BaseAdapter {
         
        
    Context context;
        
    ArrayList<ImageItem_arrayImageItem;
         
        
    MyAdapter(Context c){
         
    context c;
         
    _arrayImageItem = new ArrayList<ImageItem>();
        }
         
        public 
    void addImageItem(ImageItem item){
         
    _arrayImageItem.add(item);
        }
     
      public 
    int getCount() {
       
    // TODO Auto-generated method stub
       
    return _arrayImageItem.size();
      }
     
      public 
    Object getItem(int position) {
       
    // TODO Auto-generated method stub
       
    return _arrayImageItem.get(position);
      }
     
      public 
    long getItemId(int position) {
       
    // TODO Auto-generated method stub
       
    return position;
      }
     
      public 
    View getView(int positionView convertViewViewGroup parent) {
       
    // TODO Auto-generated method stub
       
    ImageView imageView;
       
    imageView = new ImageView(context);
        
       
    imageView.setLayoutParams(new Gallery.LayoutParams(150150));
       
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
       
    imageView.setImageBitmap(_arrayImageItem.get(position).getImage());
        
       return 
    imageView;
      }
     }
       
       public class 
    MyThread extends Thread{
        
    volatile boolean running false;
        
    int cnt;
         
        
    void setRunning(boolean b){
         
    running b;
         
    cnt 10;
        }
         
      @
    Override
      
    public void run() {
       
    // TODO Auto-generated method stub
       
    while(running){
        try {
         
    sleep(500);
         
    insertDummyImageItem(1);
         
    handler.sendMessage(handler.obtainMessage());
         if(
    cnt-- == 0){
          
    running false;
         }
        } catch (
    InterruptedException e) {
         
    e.printStackTrace();
          
        }
       }
        
      }
         
       }
       
       
    Handler handler = new Handler(){
     
      @
    Override
      
    public void handleMessage(Message msg) {
       
    // TODO Auto-generated method stub
       
    myAdapter.notifyDataSetChanged();
      }
         
       };
    }
     
    2 people like this.
    Đang tải...
  2. nguyenson.glhd New Member

    nguyenson.glhd

    Tham gia ngày:
    8/4/13
    Bài viết:
    31
    Đã được thích:
    0
    Điểm thành tích:
    0
    Giới tính:
    Nam
    Bài viết hay.
     

Chia sẻ trang này