제가 웹서버에서 이미지를 가져와 리스트뷰넣어서 화면에 출력할려고하는데(이미지 총 3개) 리스트뷰에는
마지막에 받은 이미지만 리스트에 출력합니다 .
코드 입니다
public class MainActivity extends AppCompatActivity {
TextView textView;
ImageView imageView;
Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.testview);
myThread thread=new myThread();
thread.start();
}
class myThread extends Thread {
public void run() {
Document doc = null;
String html = "http://211.226.100.190:8080/db/nemotest.jsp";
try {
URL url = new URL(html);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
doc = Jsoup.connect(html).get();
Elements img = doc.select("img");
String[] imgsrc = new String[img.size()];
OpenHttpConnection opHttpCon = new OpenHttpConnection();
for (int i = 0; i < img.size(); i++) {
imgsrc[i] = doc.select("img").get(i).getElementsByTag("img").attr("src");
println(imgsrc[i]);
}
for (String x : imgsrc) {
textView.append(x +" ");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void println(final String imgsrc)
{
handler.post(new Runnable() {
@Override
public void run() {
ArrayList<Story> al = new ArrayList<Story>();
MyAdapter adapter = new MyAdapter(
MainActivity.this, // 현재화면의 제어권자
R.layout.list_item,
al);
al.add(new Story("test","test",imgsrc));
ListView lv=(ListView)findViewById(R.id.listview);
lv.setAdapter(adapter);
}
});
}
class MyAdapter extends BaseAdapter {
Context context;
int layout;
ArrayList<Story> al;
LayoutInflater inf;
public MyAdapter(Context context, int layout, ArrayList<Story> al) {
this.context = context;
this.layout = layout;
this.al = al;
this.inf = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() { // 총 데이터의 개수
return al.size();
}
@Override
public Object getItem(int position) { // 해당 행의 데이터
return al.get(position);
}
@Override
public long getItemId(int position) { // 해당 행의 유니크한 id
return position;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
if (v == null)
v = inf.inflate(layout, null);
TextView tv1 = (TextView) v.findViewById(R.id.text1);
TextView tv2 = (TextView) v.findViewById(R.id.text2);
ImageView iv = (ImageView) v.findViewById(R.id.image);
Story s = al.get(position);
tv1.setText(s.date);
tv2.setText(s.message);
//iv.setImageResource(s.img);
ImageView bmImage;
OpenHttpConnection opHttpCon = new OpenHttpConnection();
opHttpCon.execute(iv, "http://211.226.100.190:8080/db/" + s.img);
return v;
}
}
public class Story {
String date = "";
String message = "";
String img=""; // 이미지
public Story(String date, String message, String img) {
this.date = date;
this.message = message;
this.img = img;
}
public Story() {
}
}
public class OpenHttpConnection extends AsyncTask<Object, Void, Bitmap> {
private ImageView bmImage;
@Override
protected Bitmap doInBackground(Object... params) {
Bitmap mBitmap = null;
bmImage = (ImageView) params[0];
String url = (String) params[1];
InputStream in = null;
try {
in = new java.net.URL(url).openStream();
mBitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return mBitmap;
}
@Override
protected void onPostExecute(Bitmap bm) {
super.onPostExecute(bm);
bmImage.setImageBitmap(bm);
}
}
}