ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Android 실습 (9)
    Android 2022. 7. 18. 12:02

     

    < MainActivity.java >

    package com.example.ex_0718;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ListView;
    
    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.StringRequest;
    import com.android.volley.toolbox.Volley;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.util.ArrayList;
    
    public class MainActivity extends AppCompatActivity {
    
        Button btn;
        ListView listView;
    
        // Volley 사용시 필요한 객체
        RequestQueue requestQueue; // 요청을 보내주는 객체
        StringRequest request; // 요청시 필요한 정보를 담아주는 객체
    
        // Adapter 연결시 필요한 객체
        MovieAdapter adapter;
        ArrayList<MovieVO> dataset;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btn = findViewById(R.id.btn);
            listView = findViewById(R.id.listview);
    
            requestQueue = Volley.newRequestQueue(getApplicationContext());
            dataset = new ArrayList<>();
            adapter = new MovieAdapter(getApplicationContext(), R.layout.movie_item, dataset);
    
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    request = new StringRequest(
                            Request.Method.GET,
                            "https://kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json?key=f5eef3421c602c6cb7ea224104795888&targetDt=20220717",
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject json = new JSONObject(response);
    
                                        JSONObject json2 = json.getJSONObject("boxOfficeResult");
                                        JSONArray movies = json2.getJSONArray("dailyBoxOfficeList");
    
                                        for (int i = 0; i < movies.length(); i++){
                                            JSONObject movie = (JSONObject)movies.get(i);
                                            // Log.d("check", movie.getString("movieNm"));
    
                                            String rank = movie.getString("rank");
                                            String old_new = movie.getString("rankOldAndNew");
                                            String movie_title = movie.getString("movieNm");
                                            String count = movie.getString("audiAcc");
                                            String open = movie.getString("openDt");
    
                                            dataset.add(new MovieVO(rank, old_new, movie_title,count,open));
                                        } // for문 종료
    
                                        listView.setAdapter(adapter);
    
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    Log.e("error", error.toString());
                                }
                            }
    
                    );
    
                    requestQueue.add(request);
    
                }
            });
    
        }
    }

     

    < MovieAdapter.java >

    package com.example.ex_0718;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    import org.w3c.dom.Text;
    
    import java.util.ArrayList;
    
    public class MovieAdapter extends BaseAdapter {
        // 1. Adapter 기능을 가질 수 있는 MovieAdapter 클래스 만들기
        // - 필수적으로 구현해야 하는 메소드를 불러올 수 있도록 만들기
    
        // 2. Adapter에 필요한 객체 선언
        // - 어플의 정보를 담을 수 있는 객체, 레이아웃을 담기위한 객체, 데이터셋을 담기위한 객체
        // - 레이아웃 템플릿을 xml -> viw 타입으로 변환이 가능한 객체
    
        private Context context;
        private int layout;
        private ArrayList<MovieVO> dataset;
    
        private LayoutInflater inflater;
    
        public MovieAdapter(Context context, int layout, ArrayList<MovieVO> dataset) {
            this.context = context;
            this.layout = layout;
            this.dataset = dataset;
    
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        @Override
        public int getCount() {
            return dataset.size();
        }
    
        @Override
        public Object getItem(int i) {
            return dataset.get(i);
        }
    
        @Override
        public long getItemId(int i) {
            return i;
        }
    
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
    
            view = inflater.inflate(layout, null);
    
            TextView rank = view.findViewById(R.id.rank);
            TextView old_new = view.findViewById(R.id.old_new);
            TextView movie_title = view.findViewById(R.id.movie_title);
            TextView count = view.findViewById(R.id.count);
            TextView open = view.findViewById(R.id.open);
    
            rank.setText(dataset.get(i).getRank());
            old_new.setText(dataset.get(i).getOld_new());
            movie_title.setText(dataset.get(i).getMovie_title());
            count.setText(dataset.get(i).getCount());
            open.setText(dataset.get(i).getOpen());
    
            return view;
        }
    
    
    }

     

    < MovieVO.java > 

    package com.example.ex_0718;
    
    public class MovieVO {
        // 영화 정보에 필요한 데이터를 정리하기
        private String rank;
        private String old_new;
        private String movie_title;
        private String count;
        private String open;
    
        public MovieVO(String rank, String old_new, String movie_title, String count, String open) {
            this.rank = rank;
            this.old_new = old_new;
            this.movie_title = movie_title;
            this.count = count;
            this.open = open;
        }
    
        public String getRank() {
            return rank;
        }
    
        public void setRank(String rank) {
            this.rank = rank;
        }
    
        public String getOld_new() {
            return old_new;
        }
    
        public void setOld_new(String old_new) {
            this.old_new = old_new;
        }
    
        public String getMovie_title() {
            return movie_title;
        }
    
        public void setMovie_title(String movie_title) {
            this.movie_title = movie_title;
        }
    
        public String getCount() {
            return count;
        }
    
        public void setCount(String count) {
            this.count = count;
        }
    
        public String getOpen() {
            return open;
        }
    
        public void setOpen(String open) {
            this.open = open;
        }
    }

     

    버튼을 클릭하면 영화정보가 뜨게 됩니다

     

    'Android' 카테고리의 다른 글

    Android 실습 (11)  (0) 2022.07.19
    Android 실습 (10)  (0) 2022.07.19
    Android 실습 (8)  (0) 2022.07.15
    Android 실습 (7)  (0) 2022.07.14
    Android 실습 (6)  (0) 2022.07.14

    댓글

Designed by Tistory.