구현할 내용
- json으로 저장한 모델 결과를 페이지에 나타낸다
- 7. Azure Storage에 pooling하기Azure Storage에 pooling하기에서 받아온 정보로 json 파일을 자동 생성
- 기본적인 xml은 12. 파일 목록을 ListView로 생성하기와 동일
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;
}
}
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();
}
}
}