제가 아파치 톰켓에 있는 이미지를 가져와 리스트뷰에 뿌려주고싶은데 구글링 해보니깐 커스텀 리스트뷰 가 있어서 공부하다가 막히는 부분이 있어서 이렇게 질문을 올립니다.
안드로이드에서 jsoup를 이용해 이미지 주소를 받아와 bitmap을 이용해 이미지 출력(테스트용으로 한장) 해봤는데 정상적으로 출력이 됩니다 .
그런데 제가 커스텀 리스트 뷰 를 이용해 이미지를 리스트뷰에 뿌려주고싶은데 오류가 납니다 .
아래 코드에서 lv.setAdapter(adapter) 이부분을 로그켓이 계속 잡는데 고수분들 도와주세요 ..
public class MainActivity extends AppCompatActivity {
TextView textView;
ImageView imageView;
@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://220.127.231.139:8080/db/nemotest.jsp";
try {
URL url = new URL(html);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
// doc = Jsoup.parse(output);
doc = Jsoup.connect(html).get();
Elements img = doc.select("img");
String[] imgsrc = new String[img.size()];
ArrayList<Story> al = new ArrayList<Story>();
OpenHttpConnection opHttpCon = new OpenHttpConnection();
for (int i = 0; i < img.size(); i++) {
imgsrc[i] = doc.select("img").get(i).getElementsByTag("img").attr("src");
al.add(new Story("test","test",imgsrc[i]));
}
MyAdapter adapter = new MyAdapter(
getApplicationContext(), // 현재화면의 제어권자
R.layout.list_item,
al);
//list_item > 커스텀 리스트뷰 (이미지뷰,텍스트뷰,텍스트뷰)
ListView lv = (ListView)findViewById(R.id.listview);
lv.setAdapter(adapter);
for (String x : imgsrc) {
textView.append(x + " ");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
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 convertView, ViewGroup parent) {
if (convertView == null)
convertView = inf.inflate(layout, null);
TextView tv1 = (TextView) convertView.findViewById(R.id.text1);
TextView tv2 = (TextView) convertView.findViewById(R.id.text2);
ImageView iv = (ImageView) convertView.findViewById(R.id.image);
Story s = al.get(position);
tv1.setText(s.date);
tv2.setText(s.message);
OpenHttpConnection opHttpCon = new OpenHttpConnection();
opHttpCon.execute(iv, "http://220.127.231.139:8080/db/imgFile"+s.img);
return convertView;
}
}
private 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() {}
}
}