我正在開發一個Android應用程序,該應用程序我有一個從數據庫加載數據的數據庫,包括文本和圖像。目標是這些圖像以圓形顯示,直到現在一切正常。Android Java在BaseAdapter中創建新線程無法正常工作
問題是,當我開始的活動是接收數據與AsyncTask類,並顯示「加載」對話框,但是當它傳遞給我的BaseAdapter,它會阻止,直到所有的圖像加載,是什麼讓應用程序偷懶。我能做些什麼來解決這個問題?
下面你有我所有的代碼進程。
NotificationsActivity(LoadNotifications的AsyncTask)
class LoadNotifications extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Notifications.this);
pDialog.setMessage("Loading..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("token", token));
Log.d("TOKEN", token);
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(getMyNotificationsURL,
"POST", params);
try { // Trying to get notifications
// Getting Array of notifications
notifications = json.getJSONArray("notifications");
userImage = new String[notifications.length()];
message = new String[notifications.length()];
param = new String[notifications.length()];
type = new String[notifications.length()];
timestamp = new String[notifications.length()];
// Looping through all notifications
for (int i = 0; i < notifications.length(); i++) {
JSONObject c = notifications.getJSONObject(i);
try{
userImage[i] = c.getString("userImage");
} catch(Exception e){
userImage[i] = "system";
}
message[i] = c.getString("message");
param[i] = c.getString("param");
type[i] = c.getString("type");
timestamp[i] = c.getString("timestamp");
Log.d("message", message[i]);
} // End for
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
if (notifications.length() > 0) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// dismiss the dialog after getting all videos
pDialog.dismiss();
list = (ListView) findViewById(R.id.notifications_list);
NotificationsAdapter adapter = new NotificationsAdapter(
mActivity, userImage, message, param, type,
timestamp);
list.setAdapter(adapter);
}
});
} else {
pDialog.dismiss();
Toast.makeText(getApplicationContext(),
getString(R.string.noNotifications), Toast.LENGTH_LONG)
.show();
finish();
}
} // Close PostExecute
} // Close LoadNotifications
NotificationsAdapter(該在構造函數週期是什麼加載圖片)
public class NotificationsAdapter extends BaseAdapter{
private Activity activity;
private String[] usersImage, messages, params, types, timestamps;
private Bitmap[] loadedImages;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public boolean isOnline() {
ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
public NotificationsAdapter(final Activity activity, String[] userImage, String[] messages, String[] params, String[] types, String[] timestamps) {
this.activity = activity;
this.usersImage = userImage;
this.messages = messages;
this.params = params;
this.types = types;
this.timestamps = timestamps;
final ImageHelper imageHelper = new ImageHelper();
loadedImages = new Bitmap[getCount()];
/*(new Thread(new Runnable() {
@Override
public void run() {
activity.runOnUiThread(new Runnable() {
public void run() {*/
for (int i=0; i<getCount(); i++){
loadedImages[i] = imageHelper.downloadImageTaskOnly(usersImage[i]);
}
/*areImagesLoaded=true;
}
});
}
})).start();*/
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return messages.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageHelper imageHelper = new ImageHelper();
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.notifications_item, null);
ImageView userImage = (ImageView) vi.findViewById(R.id.userImage);
TextView message = (TextView) vi.findViewById(R.id.notification_message);
TextView param = (TextView) vi.findViewById(R.id.notification_param);
TextView type = (TextView) vi.findViewById(R.id.notification_type);
TextView timestamp = (TextView) vi.findViewById(R.id.notification_timestamp);
if (!isOnline()) {
Toast.makeText(activity,
activity.getString(R.string.noInternetConnection),
Toast.LENGTH_SHORT).show();
} else {
if(usersImage[position].equalsIgnoreCase("system")){
Bitmap icon = BitmapFactory.decodeResource(activity.getResources(), R.drawable.vidytape_icon);
userImage.setImageBitmap(imageHelper.BigRoundedShape(icon));
} else {
userImage.setImageBitmap(imageHelper.BigRoundedShape(loadedImages[position]));
}
}
message.setText(messages[position]);
param.setText(params[position]);
type.setText(types[position]);
timestamp.setText(timestamps[position]);
return vi;
}
}
ImageHelper
downloadImageTaskOnly方法
public Bitmap downloadImageTaskOnly(String url){
Log.e("IMAGE HELPER", url);
try {
return new DownloadImageTaskOnly().execute(url).get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
downloadImageTaskOnly的AsyncTask
private class DownloadImageTaskOnly extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
Log.e("IMAGE HELPER", "DOWNLOAD ONLY");
String urldisplay = urls[0];
Log.e("IMAGE HELPER", urldisplay);
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
} // Close DownloadImageTaskOnly
BigRoundedShape
public Bitmap BigRoundedShape(Bitmap scaleBitmapImage) {
// TODO Auto-generated method stub
int targetWidth = 250;
int targetHeight = 300;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1)/2,
((float) targetHeight - 1)/2,
(Math.min(((float) targetWidth), ((float) targetHeight))/2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}
我的目標是能夠在用戶使用的應用程序,並且沒有圖像已經加載,仍然加載回來地面
如何在我的項目中包含畢加索? – user3042700 2014-08-28 01:14:23
我已經在我的項目中包含了picasso庫。它工作!非常感謝 – user3042700 2014-08-28 01:33:36