안드로이드 커스텀 리스트뷰(listview)
public class MainActivity extends AppCompatActivity {ListView listView;ArrayList<Student> data = null;ListAdapter list = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView) findViewById(R.id.listView);data = new ArrayList<Student>();for(int i=0; i<15; i++){Student student = new Student();student.setsName("학생" + Integer.toString(i));student.setsNum(Integer.toString(i));student.setsDepartment("컴퓨터공학부" + Integer.toString(i));data.add(student);}list = new ListAdapter(this, data);listView.setAdapter(list);listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(getApplicationContext(), "클릭 : " + position, Toast.LENGTH_SHORT).show();}});}}
** ListAdapter.java
public class ListAdapter extends BaseAdapter {
Context mContext;
ArrayList<Student> mData;
LayoutInflater inflater;
public ListAdapter(Context context, ArrayList<Student> data) {
mContext = context;
mData = data;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Student getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = inflater.inflate(R.layout.activity_list, parent, false);
}
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView num = (TextView) convertView.findViewById(R.id.num);
TextView department = (TextView) convertView.findViewById(R.id.department);
name.setText(mData.get(position).getsName());
num.setText(mData.get(position).getsNum());
department.setText(mData.get(position).getsDepartment());
return convertView;
}
}
** student.java
public class Student {
private String sName;
private String sNum;
private String sDepartment;
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getsNum() {
return sNum;
}
public void setsNum(String sNum) {
this.sNum = sNum;
}
public String getsDepartment() {
return sDepartment;
}
public void setsDepartment(String sDepartment) {
this.sDepartment = sDepartment;
}
}
** activity_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<TextView
android:id="@+id/num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_below="@+id/department"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/department"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_below="@+id/name"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
** 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="com.example.dmbtv.middle_test.MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
** 실행화면
도움되셨다면 공감 꾹! 눌려주세요~
'Android' 카테고리의 다른 글
안드로이드) MQTT를 이용한 스마트홈 구성 (1) | 2018.02.11 |
---|---|
안드로이드 Mqtt 통신 (0) | 2017.11.20 |
안드로이드 ColorPicker 라이브러리 색상 선택 (0) | 2017.09.25 |
안드로이드 스튜디오 - 라이브러리 추가(import) (0) | 2017.09.22 |
SmartHome 어플 제작 - 무드등 탭 (0) | 2017.09.17 |