마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 커스텀 리스트뷰 관련 문의드립니다.

0 추천

제가 아파치 톰켓에 있는 이미지를  가져와 리스트뷰에 뿌려주고싶은데  구글링 해보니깐 커스텀 리스트뷰 가 있어서 공부하다가 막히는 부분이 있어서 이렇게 질문을 올립니다.

안드로이드에서 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() {}



        }

    }

kane9529 (1,110 포인트) 님이 2019년 5월 17일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...