json 데이터로 ListView 생성하기

Hwan·2023년 2월 20일
0

voicekeeper

목록 보기
13/16

구현할 내용

1. CustomAdapter.java

  • 이전에 만들었던 것처럼 Adapter 생성
  • 이번에는 별개의 class를 생성하여 사용
  • json 파일의 특정 데이터를 이용하여 listview 생성 (getView)
public class CustomAdapter extends BaseAdapter {
    private Context applicationContext;
    private int sample;
    private List<jsonModel> jsonModels;

    CustomAdapter(Context applicationContext, int sample, List<jsonModel> jsonModels) {
        this.applicationContext = applicationContext;
        this.sample = sample;
        this.jsonModels = jsonModels;
    }

    @Override
    public int getCount() {
        return jsonModels.size();    }
    @Override
    public Object getItem(int i) {
        return jsonModels.get(i);    }
    @Override
    public long getItemId(int i) {
        return i;    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if(view == null)  {
            LayoutInflater layoutInflater = (LayoutInflater) applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view =  layoutInflater.inflate(R.layout.result_item,viewGroup,false);
        }
        TextView phoneNum,when,duration;

        phoneNum= view.findViewById(R.id.phoneNum);
        when=view.findViewById(R.id.when);
        duration=view.findViewById(R.id.duration);

        String jsonTime = jsonModels.get(i).getInfo().getTime();
        int hh = Integer.parseInt(jsonTime.substring(0,2));
        String mm = jsonTime.substring(2);
        String ampm = "";
        if (hh > 12){
            hh = hh - 12;
            ampm = "오후";
        } else{
            ampm = "오전";}

        phoneNum.setText(jsonModels.get(i).getInfo().getNumber());
        when.setText(ampm + " " +hh + ":" + mm);
        duration.setText(jsonModels.get(i).getInfo().getDuration());

        return view;
    }
}

2. RecordListActivity.java

  • "data.json"에 있는 정보값을 읽어온다
  • 나타내는 형식은 CustomAdapter에 지정되어 있음
public class RecordListActivity extends AppCompatActivity {

    private ListView listView; // 리스트뷰

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_list);
        listView = findViewById(R.id.my_listView);

        try {
            InputStream is = getApplicationContext().openFileInput("data.json");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            reader.close();
            is.close();
            String jsonStr = stringBuilder.toString();

            Gson gson = new Gson();
            Type listType = new TypeToken<List<jsonModel>>(){}.getType();
            List<jsonModel> dataList = gson.fromJson(jsonStr, listType);

            CustomAdapter adapter = new CustomAdapter(getApplicationContext(), R.layout.result_item, dataList);
            listView.setAdapter(adapter);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

0개의 댓글