我这里有个问题,这是我customAdapter
的 for ListView
。我试图获取一个 XML 文件并使用此适配器显示它。
我的 XML 文件源很少,其中一个有图像链接。使用此代码,我显示了所有图像,但不知何故它没有放在正确的imageView
.
谁能明白为什么这不能正常工作?
package dotmanga.classes;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import example.dotmanga.R;
public class TestAdapter extends BaseAdapter {
private Context con;
private List<Entry> entries;
private LayoutInflater inflater;
private Handler hand = new Handler();
static class ViewHolder {
TextView title;
TextView link;
ImageView imv;
}
public TestAdapter(Context c, List<Entry> e) {
this.con = c;
this.entries = e;
this.inflater = LayoutInflater.from(this.con);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return entries.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return entries.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
View vi = convertView;
if (convertView == null) {
vi = this.inflater.inflate(R.layout.activity_list_row, null);
holder = new ViewHolder();
holder.title = (TextView) vi.findViewById(R.id.title);
holder.link = (TextView) vi.findViewById(R.id.link);
holder.imv = (ImageView) vi.findViewById(R.id.imageView1);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
Entry entry = entries.get(position);
holder.title.setText(entry.title);
holder.link.setText(entry.link);
if (entry.img != null) {
final String myurl = entry.img;
new Thread(new Runnable() {
public void run() {
try {
URL url = new URL(myurl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
final Bitmap myBitmap = BitmapFactory.decodeStream(input);
hand.post(new Runnable() {
public void run() {
holder.imv.setImageBitmap(myBitmap);
}
});
connection.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
return vi;
}
}