Android
안드로이드 - 커스텀 리스트뷰에 데이터베이스 연결
남자두부 2015. 6. 16. 23:06반응형
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
86
87
88
89
90
91
92
93
94
95
96
97
98 |
public void test() {
ArrayList<MyData> data = new ArrayList<MyData>();
DataBase DB = new DataBase(this);
DataBase.cursor = DB.QueryData();
DataBase.cursor.moveToFirst();
for(int i = 0; i < DataBase.cursor.getCount(); i++) {
MyData myData;
myData = new MyData(
DataBase.cursor.getString(1),
DataBase.cursor.getString(2),
DataBase.cursor.getString(3));
data.add(myData);
DataBase.cursor.moveToNext();
}
MyDataAdapter Adapter =
new MyDataAdapter(this, R.layout.list_item, data);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(Adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DataBase.cursor.moveToPosition(position);
Log.d("msg : ", DataBase.cursor.getString(3));
}
});
}
class MyData {
String day, time, memo;
MyData(String aDay, String aTime, String aMemo) {
day = aDay;
time = aTime;
memo = aMemo;
}
}
class MyDataAdapter extends BaseAdapter {
Context acontext;
int alistItem;
ArrayList<MyData> adata;
LayoutInflater inflacter;
public MyDataAdapter(Context context, int listItem, ArrayList<MyData> data) {
acontext = context;
adata = data;
alistItem = listItem;
inflacter = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return adata.size();
}
@Override
public Object getItem(int position) {
return adata.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflacter.inflate(alistItem, parent, false);
}
TextView day = (TextView) convertView.findViewById(R.id.textView1);
day.setText(adata.get(position).day);
TextView time = (TextView) convertView.findViewById(R.id.textView2);
time.setText(adata.get(position).time);
TextView memo = (TextView) convertView.findViewById(R.id.textView3);
memo.setText(adata.get(position).memo);
final Button btn = (Button) convertView.findViewById(R.id.button1);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
btn.setVisibility(View.INVISIBLE);
}
});
return convertView;
}
} |
cs |
백업용
반응형