2. 리스트뷰로 데이터를 어떻게 디자인하여 띄울 것인가에 따라 Layout 구성(list_item.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="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="100dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/contenttText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
3. 새 클래스에 리스트뷰로 띄울 데이터들을 정의한다(ListViewItem)
public class ListViewItem { // getter / setter 사용
private Drawable partimage;
private String titleStr;
private String contentStr;
public Drawable getPartimage() {
return partimage; }
public void setPartimage(Drawable partimage) {
this.partimage = partimage; }
public String getTitleStr() {
return titleStr; }
public void setTitleStr(String titleStr) {
this.titleStr = titleStr; }
public String getContentStr() {
return contentStr; }
public void setContentStr(String contentStr) {
this.contentStr = contentStr; }
}
4. BaseAdapter 상속받아 새로운 어댑터 생성(MyAdapter.class)
public class MyAdapter extends BaseAdapter {
ArrayList arrList = new ArrayList() ;
public MyAdapter(){ } // 생생자
public View getView(int position, View convertView, ViewGroup parent){ // getView를 잘 활용하는 것이 중요
Context context = parent.getContext();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
ImageView partImage = (ImageView) convertView.findViewById(R.id.imageView) ;
TextView titleText = (TextView) convertView.findViewById(R.id.titleText) ;
TextView contenttText = (TextView) convertView.findViewById(R.id.contenttText) ;
ListViewItem listViewItem = (ListViewItem) arrList.get(position);
partImage.setImageDrawable(listViewItem.getPartimage());
titleText.setText(listViewItem.getTitleStr());
contenttText.setText(listViewItem.getContentStr());
return convertView;
}
public long getItemId(int position) {
return position ; }
public Object getItem(int position) {
return arrList.get(position) ; }
public int getCount() {
return arrList.size() ; }
// 아이템 데이터 추가를 위한 함수
public void addItem(Drawable partImage, String titleText, String contenttText) {
ListViewItem item = new ListViewItem();
item.setPartimage(partImage);
item.setTitleStr(titleText);
item.setContentStr(contenttText);
arrList.add(item);
}
}
5. 리스트뷰를 어댑터에 붙이고 데이터 삽입(MainAcitivity.class)
public class MainActivity extends AppCompatActivity {
ListView listView;
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
adapter = new MyAdapter();
listView.setAdapter(adapter);
adapter.addItem(getResources().getDrawable(R.drawable.cat),
"고양이", "고양이는 귀여워") ;
adapter.addItem(getResources().getDrawable(R.drawable.croker),
"악어", "악어는 사나워") ;
adapter.addItem(getResources().getDrawable(R.drawable.dog),
"강아지", "광견병 조심") ;
adapter.addItem(getResources().getDrawable(R.drawable.ele),
"코끼리", "너무 커") ;
adapter.addItem(getResources().getDrawable(R.drawable.monkey),
"원숭이", "바나나 조아해") ;
adapter.addItem(getResources().getDrawable(R.drawable.rabbit),
"토끼", "귀가 이뻐");
adapter.addItem(getResources().getDrawable(R.drawable.rion),
"사자", "맹수지");
// 리스트뷰 이벤트 처리
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListViewItem item = (ListViewItem)parent.getItemAtPosition(position);
Drawable partImage = item.getPartimage();
String titleStr = item.getTitleStr();
String contentStr = item.getContentStr();
Toast.makeText(MainActivity.this, titleStr + " : " + contentStr, Toast.LENGTH_SHORT).show();
}
});
}
}