Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

android - how to send ArrayList(Bitmap) from asyncTask to Fragment and use it in Arrayadapter

I want to send a list of bitmap i retreived from mysql database using asyncTask to the fragment Fragment_ListView.class, in this fragment class i want to set the adapter of the listView with the bitmap token from asyncTask but i don't know how to do that.

Async Task

 @Override
protected void onPostExecute(ArrayList<Bitmap> bitmapArrayList) {
    super.onPostExecute(bitmapArrayList);
    loading.dismiss();
    // now after getting images from server , i want to send this bitmapArrayList 
    // to Fragment_ListView where i set the adapter of the 
}

@Override
protected ArrayList<Bitmap> doInBackground(String... params) {
    imageList = new ArrayList();
    String add1 = "http://192.168.1.11/save/load_image_from_db.php?id=1";
    String add2 = "http://192.168.1.11/save/load_image_from_db.php?id=2";
    String add3 = "http://192.168.1.11/save/load_image_from_db.php?id=3";
    URL url;
    Bitmap image = null;
    String[] adds = {add1, add2, add3};
    for (int i = 0; i < adds.length; i++) {
        try {
            url = new URL(adds[i]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            image = BitmapFactory.decodeStream(connection.getInputStream());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        imageList.add(image);
        image = null;
    }
    return imageList;

OnCreate of MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listfrg = new Fragment_ListView();
    getFragmentManager().beginTransaction().add(R.id.frml, listfrg).commit();
}

Fragment_ListView :

public class Fragment_ListView extends Fragment {

ListView mListView;
static ArrayList<Bitmap> bitmaps;
static MySimpleArrayAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frglist, container, false);
    mListView = (ListView) view.findViewById(R.id.listView);
    bitmaps = new ArrayList<>();
    adapter = new MySimpleArrayAdapter(getActivity(), bitmaps);
    mListView.setAdapter(adapter);
    return view;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something like this,

Create a new interface file

public interface BitMapArrayCallBack {
    abstract void bitmapArray(ArrayList<Bitmap> bitmaps);
}

Then in AsyncTask ... i don't know your class name so i will assume the class ServerRequest

public class ServerRequest {
   Context context;

   public ServerRequest(Context context) {
        this.context = context;
   }

   public void doAsyncTask(BitMapArrayCallBack bitmapCallback) {
         new MyAsyncTask(bitmapCallback).execute();
   }


   public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
        private BitMapArrayCallBack bitmapCallback;
        public MyAsyncTask(BitMapArrayCallBack bitmapCallback) {
            this.bitmapCallback = bitmapCallback;
        }
        //call do in background etc..
     @Override
        protected void onPostExecute(ArrayList<Bitmap> bitmapArrayList) {
            super.onPostExecute(bitmapArrayList);
            loading.dismiss();
            bitmapCallback.bitmapArray(bitmapArrayList);
            // This will hold and return your arraylist
        }
    }

}

Now you must call your asynctask in fragment listview

public class Fragment_ListView extends Fragment {

ListView mListView;
static MySimpleArrayAdapter adapter;
private ServerRequest serverRequest;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frglist, container, false);
    mListView = (ListView) view.findViewById(R.id.listView);

    serverRequest = new ServerRequest(getActivity);

    serverRequest.doAsyncTask(new BitMapArrayCallBack {
        @Override
        void bitMapArray(ArrayList<Bitmap> bitmaps) {
             // in here you have access to the bitmaps array

             adapter = new MySimpleArrayAdapter(getActivity(), bitmaps);
             mListView.setAdapter(adapter);
        }

    })


    return view;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...