让我试着用一个例子来回答这个问题:)- MyImageSearch[请参考这里的主活动屏幕的图像-包含编辑文本/搜索按钮/网格视图]
MyImageSearch -一旦用户在编辑文本字段输入详细信息并单击搜索按钮,我们将通过flickr提供的web服务在互联网上搜索图像(您只需要在那里注册以获得密钥/秘密令牌)-对于搜索,我们发送一个HTTP请求和get JSON数据返回响应,其中包含个人图像的url,然后我们将使用它来加载网格视图。
My Implementation - In the main activity I will define a inner class which extends the AsyncTask to send the HTTP Request in doInBackGround Method and fetch the JSON Response and update my local ArrayList of FlickrItems which I am going to use to update my GridView via the FlickrAdapter (extends the BaseAdapter) and call the adapter.notifyDataSetChanged() in the onPostExecute() of AsyncTask to reload the grid view. Note that here the HTTP Request is a blocking call because of which I have done it via the AsyncTask. And, I can cache the items in adapter to increase the performance or store them on SDCard. The grid that I will be inflating in the FlickrAdapter contains in my implementation a progressbar and image view. Below you can find the code for mainActivity which I used.
现在回答问题-
因此,一旦我们获得了用于获取单个图像的JSON数据,我们就可以实现通过Handlers或线程或AsyncTask在后台获取图像的逻辑。我们应该注意到,由于我的图像一旦下载就必须显示在UI/主线程上,我们不能简单地使用线程,因为它们不能访问上下文。
在FlickrAdapter中,我能想到的选择:
Choice 1: Create a LooperThread [extends thread] - and keep on
downloading images sequentially in one thread by keeping this thread
open [looper.loop()]
Choice 2: Make use of a Thread Pool and post the runnable via myHandler which
contains reference to my ImageView, but since the views in Grid View
are recycled, again the problem might arise where image at index 4 is
displayed at index 9 [download may take more time]
Choice 3 [I used this]: Make use of a Thread Pool and send a message to myHandler, which contains data related to ImageView's index and
ImageView itself, so while doing handleMessage() we will update the
ImageView only if currentIndex matches the index of the Image we
tried to download.
Choice 4: Make use of AsyncTask to download the
images in background, but here I will not have access to the number of threads I want in
the thread pool and it varies with different android version, but in Choice 3 I can make of conscious decision of the size of thread pool depending on device configuration being used.
以下是源代码:
public class MainActivity extends ActionBarActivity {
GridView imageGridView;
ArrayList<FlickrItem> items = new ArrayList<FlickrItem>();
FlickrAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageGridView = (GridView) findViewById(R.id.gridView1);
adapter = new FlickrAdapter(this, items);
imageGridView.setAdapter(adapter);
}
// To avoid a memory leak on configuration change making it a inner class
class FlickrDownloader extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
FlickrGetter getter = new FlickrGetter();
ArrayList<FlickrItem> newItems = getter.fetchItems();
// clear the existing array
items.clear();
// add the new items to the array
items.addAll(newItems);
// is this correct ? - Wrong rebuilding the list view and should not be done in background
//adapter.notifyDataSetChanged();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
adapter.notifyDataSetChanged();
}
}
public void search(View view) {
// get the flickr data
FlickrDownloader downloader = new FlickrDownloader();
downloader.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我希望我的回答虽然很长,但将有助于理解一些更精细的细节。