반응형
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 |
package com.ktds.mylistview2;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Items> listItems = new ArrayList<Items>();
Items item = null;
for(int i=0; i<50; i++){
item = new Items();
item.setImageResource(R.mipmap.ic_launcher);
item.setText((i + 1) + "");
listItems.add(item);
}
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new MyListViewAdopter(listItems,this));
}
}
|
cs |
MyListViewAdopter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 |
package com.ktds.mylistview2;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by 206-001 on 2015-06-18.
*/
public class MyListViewAdopter extends BaseAdapter {
private List<Items> listItems;
private Context context;
public MyListViewAdopter(List<Items> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
/**
* ListView에 보여질 Item의 개수
* @return
*/
@Override
public int getCount() {
return listItems.size();
}
/**
* ListView에 보여질 각각의 Item
* @param position
* @return
*/
@Override
public Object getItem(int position) {
return listItems.get(position);
}
/**
* ListView에 보여질 각각의 Item들의 id
* @param position
* @return
*/
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item,parent,false);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.imgView);
TextView tvText = (TextView) convertView.findViewById(R.id.tvText);
Items item = (Items) getItem(position);
imageView.setImageResource(item.getImageResource());
tvText.setText(item.getText());
return convertView;
}
}
|
cs |
Items.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 |
package com.ktds.mylistview2;
/**
* Created by 206-001 on 2015-06-18.
*/
public class Items {
private int imageResource;
private String text;
public int getImageResource() {
return imageResource;
}
public void setImageResource(int imageResource) {
this.imageResource = imageResource;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
|
cs |
acticity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
|
cs |
list_item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imgView"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvText"
android:text="Test"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout> |
cs |
Holder class를 활용하여 ListView 구현
Holder class : layout에 들어가는 widget을 담는 역할
MyListViewAdopter.java 수정 및 MyItemsHolder.java 추가
나머지 파일은 위와 같음
MyListViewAdopter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 |
package com.ktds.mylistview2;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by 206-001 on 2015-06-18.
*/
public class MyListViewAdopter extends BaseAdapter {
private List<Items> listItems;
private Context context;
public MyListViewAdopter(List<Items> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
/**
* ListView에 보여질 Item의 개수
* @return
*/
@Override
public int getCount() {
return listItems.size();
}
/**
* ListView에 보여질 각각의 Item
* @param position
* @return
*/
@Override
public Object getItem(int position) {
return listItems.get(position);
}
/**
* ListView에 보여질 각각의 Item들의 id
* @param position
* @return
*/
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyItemsHolder holder = null;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item,parent,false);
holder = new MyItemsHolder();
holder.imgView = (ImageView) convertView.findViewById(R.id.imgView);
holder.tvText = (TextView) convertView.findViewById(R.id.tvText);
}
else{
holder = (MyItemsHolder) convertView.getTag();
}
/*ImageView imageView = (ImageView) convertView.findViewById(R.id.imgView);
TextView tvText = (TextView) convertView.findViewById(R.id.tvText);*/
Items item = (Items) getItem(position);
holder.imgView.setImageResource(item.getImageResource());
holder.tvText.setText(item.getText());
/*imageView.setImageResource(item.getImageResource());
tvText.setText(item.getText());*/
convertView.setTag(holder);
return convertView;
}
}
|
cs |
MyItemsHolder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
package com.ktds.mylistview2;
import android.widget.ImageView;
import android.widget.TextView;
/**
* layout에 들어가는 widget을 담는 역할
* Created by 206-001 on 2015-06-18.
*/
public class MyItemsHolder {
public ImageView imgView;
public TextView tvText;
}
|
cs |
반응형
'IT > Android' 카테고리의 다른 글
1. Activity를 이용한 계산기 (0) | 2015.06.22 |
---|---|
Layout (0) | 2015.06.22 |
Container - TabHost (0) | 2015.06.18 |
Widget - WebView (0) | 2015.06.17 |
Widget - Layout (0) | 2015.06.17 |