2016-03-23 50 views
-1

我想給位圖的列表我使用的AsyncTask的片段Fragment_ListView.class從MySQL數據庫retreived,在這個片段類,我想使用asyncTask中的位圖標記設置listView的適配器,但我不知道該怎麼做。如何從發送的AsyncTask的ArrayList(位圖)到碎片,並用它在Arrayadapter

異步任務

@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中的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; 
} 

回答

0
舒美特

興這樣,

中的AsyncTask創建一個新的接口文件

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

然後...我不知道你的類名,所以我會假設類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 
     } 
    } 

} 

現在你必須在片段列表視圖中調用您的asynctask

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; 
} 
相關問題