I have the following code in which information about images is loaded from a db and shown in a custom gallery view. The problem is I cannot see a way to put the setting on the OnItemClickListener for the gallery in the background thread of the AsyncTask as it is part of the main UI thread (I think?). When I load the Activity the dialog pops up but freezes and I think it's because I am setting the OnItemClickListener in the UI thread which is taking a lot of work. How can I solve this problem?
private class loadTask extends AsyncTask<String, Integer, String>{
ProgressDialog dialog = new ProgressDialog(ActivityGallery.this);
@Override
protected void onPreExecute(){
b = getIntent().getExtras();
dialog.setMessage("Loading...");
dialog.show();开发者_运维技巧
}
@Override
protected String doInBackground(String... params) {
Log.v("SC", "ASYNC GETS HERE!!");
DBHandler handler = new DBHandler(ActivityGallery.this);
SQLiteDatabase db = handler.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM images", null);
if (c.moveToFirst()) {
do {
paths.add(c.getString(1));
Log.v("SC", "Cursor: " + c.getString(1));
} while (c.moveToNext());
}
c.close();
db.close();
handler.close();
return null;
}
@Override
protected void onProgressUpdate(Integer... s){
}
@Override
protected void onPostExecute(String result){
Log.v("SC", "Final Paths: " + paths.toString());
setContentView(R.layout.gallery);
gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(ActivityGallery.this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
selected_image_position = position;
GoToShareActivity();
}
});
dialog.dismiss();
}
}
I think GoToShareActivity() causes freezing. Check that function in detail.
What does GoToShareActivity() do?
If it's another resource intensive operation, then it will cause the dialog to freeze up. This is because onPostExecute() runs on the UI thread.
I've a similar program that uses asynctask like you do. I've tried putting my onItemClickListener() in onPostExecute but it doesn't freeze the dialog as i'm not doing any heavy process in it. I understand the part whereby you want to show the gallery to the user after the dialog box dismisses.
ANSWER: You can do the gallery.setOnItemClickListener() in there but, the actual execution of the onClick() should not be in there.
@Override
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.gallery); //place before your loadTask()
.
. //your other implementations
.
}
private OnItemClickListener galleryOnItemClickListener = new OnItemClickListener()
{
@Override
public void onClick(View v)
{
selected_image_position = position;
GoToShareActivity();
}
};
private class loadTask extends AsyncTask<String, Integer, String>
{
.
.
.
protected void onPostExecute(String result)
{
Log.v("SC", "Final Paths: " +paths.toString());
gallery = (Gallery)findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(ActivityGallery.this));
gallery.setOnItemClickListener(galleryOnItemClickListener);
dialog.dismiss();
}
}
精彩评论