谁能看到我在这里做错了什么?我知道这是很多代码,但除了图像视图中没有出现图像之外我没有任何其他错误。图像视图显示但空白。
我想我在错误的地方运行asynctask什么的?
以下是代码:
MainActivity.java:
package se.bjornange.android.grid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
public class mainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
}
}
ImageAdapter.java
package se.bjornange.android.grid;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter implements NetworkCallListener {
private Context mContext;
Bitmap thebmp;
ImageView imageView;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return aThumbUrls.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public void onNetworkCallComplete(Bitmap bmp) {
thebmp = bmp;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
new NetworkCallTask(this).execute(aThumbUrls[position]);
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(thebmp);
return imageView;
}
private String[] aThumbUrls = { "http://www.google.se/images/logo_sm.gif",
"http://www.google.se/images/logo_sm.gif", "http://www.google.se/images/logo_sm.gif"
};
@Override
public void onNetworkCallCancel() {
// TODO Auto-generated method stub
}
}
NetworkCallListener.java
package se.bjornange.android.grid;
import android.graphics.Bitmap;
public interface NetworkCallListener {
public void onNetworkCallComplete(Bitmap bmp);
public void onNetworkCallCancel();
}
NetworkCallTask.java
package se.bjornange.android.grid;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
public class NetworkCallTask extends AsyncTask<String, Void, Bitmap> {
NetworkCallListener listener = null;
String url;
public NetworkCallTask(NetworkCallListener listener) {
this.listener = listener;
}
protected void onPreExecute() {
}
protected Bitmap doInBackground(String... params) {
Bitmap bmImg = null;
url = params[0];
URL myFileUrl = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmImg;
}
protected void onPostExecute(final Bitmap bmImg) {
//imageView.setImageBitmap(bmImg);
listener.onNetworkCallComplete(bmImg);
}
}
答案 0 :(得分:1)
答案 1 :(得分:0)
我不认为你可以在用imageView.setImageBitmap(thebmp)设置位图之后分配位图;您应该在onNetworkCallComplete()函数中执行imageView.setImageBitmap();
尝试一下这个:
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final ImageView imageV = convertView == null ? new ImageView(this) : (ImageView) convertView;
new AsyncTask(){
Bitmap bitmap;
@Override
protected Object doInBackground(Object... params) {
bitmap = load your bitmap here
return null;
}
@Override
protected void onPostExecute(Object result) {
imageV.setImageBitmap(bitmap);
}
}.execute(null);
return imageV;
}