Android 리스트뷰
2019. 12. 28. 21:38
Programing/Android
- activity_main.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".List_Main">
<TextView
android:id="@+id/btnHome"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="@drawable/bg"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="30sp"
app:fontFamily="sans-serif-condensed-light" />
<View
android:id="@+id/view4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="NO"
android:textColor="#E91E63"
android:textSize="18sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="DATE"
android:textColor="#E91E63"
android:textSize="18sp" />
<TextView
android:id="@+id/textView22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="START"
android:textColor="#E91E63"
android:textSize="18sp" />
<TextView
android:id="@+id/textView23"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="END"
android:textColor="#E91E63"
android:textSize="18sp" />
</LinearLayout>
<View
android:id="@+id/view5"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="450dp">
<ListView
android:id="@+id/listView_main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="샴푸"
android:textColor="#000000"
android:textSize="60dp" />
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="트리트먼트"
android:textColor="#000000"
android:textSize="60dp" />
<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="바디워시"
android:textColor="#000000"
android:textSize="60dp" />
</LinearLayout>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="horizontal">
<TextView
android:id="@+id/tvSam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="90dp" />
<TextView
android:id="@+id/tvTri"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="90dp" />
<TextView
android:id="@+id/tvBod"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="90dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/btnReSam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="리필"
android:textColor="#ffffff" />
<Button
android:id="@+id/btnReTri"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="리필"
android:textColor="#ffffff" />
<Button
android:id="@+id/btnReBod"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:text="리필"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
- List_Main.java
더보기
public class List_Main extends AppCompatActivity {
private SwipeRefreshLayout mSwipeRefreshLayout;
private ListAdapter adapter;
private ListView listView;
private String result_data;
int LastInt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_main);
listView = (ListView) findViewById(R.id.listView_main_list);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mSwipeRefreshLayout.setRefreshing(false);
}
});
}
public void setList(){
for (int i = 0; i < jsonArray.length(); i++) {
//데이터 가져와 PersonalData라는 Class의 생성자를 통해 변수에 데이터를 넣는다. 한번에 데이터를 전달달하
PersonalData personalData = new PersonalData();
personalData.setIndex_no(i + 1 + "");
personalData.setS_date(s_date);
personalData.setStime(stime);
personalData.setEtime(etime);
//데이터 어뎁터에 추가
adapter.addItem(personalData);
}
//리스트뷰에 어뎁터 적용
listView.setAdapter((android.widget.ListAdapter) adapter);
}
}
- item_list_data.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_list_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="@+id/textView_list_date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="@+id/textView_list_stime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="@+id/textView_list_etime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>
<!--
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="SHAMPOO"
android:textColor="#E91E63"
android:textSize="18sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="RINSE"
android:textColor="#E91E63"
android:textSize="18sp" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="BODY"
android:textColor="#E91E63"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_list_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="@+id/textView_list_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="@+id/textView_list_tree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>
!-->
</LinearLayout>
- ListAdapter.java
더보기
package kr.ac.jj.hackathon113app;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ListAdapter extends BaseAdapter {
private ArrayList<PersonalData> listCustom = new ArrayList<>();
// ListView에 보여질 Item 수
@Override
public int getCount() {
return listCustom.size();
}
// 하나의 Item(ImageView 1, TextView 2)
@Override
public Object getItem(int position) {
return listCustom.get(position);
}
// Item의 id : Item을 구별하기 위한 것으로 position 사용
@Override
public long getItemId(int position) {
return position;
}
// 실제로 Item이 보여지는 부분
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CustomViewHolder holder = new CustomViewHolder();
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_data, null, false);
//layout.item_list_data.xml에 있는 텍스트뷰 변수로 연결
holder.Index_no = (TextView) convertView.findViewById(R.id.textView_list_id);
holder.s_date = (TextView) convertView.findViewById(R.id.textView_list_date);
holder.s_stime = (TextView) convertView.findViewById(R.id.textView_list_stime);
holder.e_stime = (TextView) convertView.findViewById(R.id.textView_list_etime);
convertView.setTag(holder);
} else {
holder = (CustomViewHolder) convertView.getTag();
}
//PersonalData 생성자 불러 변수에 추가
PersonalData dto = listCustom.get(position);
holder.Index_no.setText(dto.getIndex_no());
holder.s_date.setText(dto.getS_date());
holder.s_stime.setText(dto.getStime());
holder.e_stime.setText(dto.getStime());
return convertView;
}
class CustomViewHolder {
//텍스트뷰 변수선언
TextView Index_no;
TextView s_date;
TextView s_stime;
TextView e_stime;
}
// MainActivity에서 Adapter에있는 ArrayList에 data를 추가시켜주는 함수
public void addItem(PersonalData dto) {
listCustom.add(dto);
}
}
- PersonalData.java
더보기
public class PersonalData {
private String index_no;
private String s_date;
private String stime;
private String etime;
public PersonalData(String index_no, String s_date, String stime, String etime) {
this.index_no = index_no;
this.s_date = s_date;
this.stime = stime;
this.etime = etime;
}
public PersonalData() {
}
public String getEtime() {
return etime;
}
public void setEtime(String etime) {
this.etime = etime;
}
public String getIndex_no() {
return index_no;
}
public void setIndex_no(String index_no) {
this.index_no = index_no;
}
public String getS_date() {
return s_date;
}
public void setS_date(String s_date) {
this.s_date = s_date;
}
public String getStime() {
return stime;
}
public void setStime(String stime) {
this.stime = stime;
}
}
'Programing > Android' 카테고리의 다른 글
Android Dialog 화면 꽉채우기 (0) | 2020.12.07 |
---|---|
Android HTML, 외부주소에서 JSON 파싱해 가져오기 (0) | 2019.12.28 |
assets 사전 insert DB (0) | 2019.12.09 |
Adapter, GridView와 ListView 적용법 (0) | 2019.11.25 |
FCM 앱 알람 (0) | 2019.11.02 |