안드로이드/프로그래밍

[안드로이드] 커스텀 리스트뷰 만들기 #1 [연결]

RBWSN 2015. 2. 28. 20:10
728x90
오늘은 커스텀 리스트뷰를 만들어 보겠다.


커스텀 리스트뷰에는 
리스트뷰에 올릴 커스텀 레이아웃

+
그리고 리스트뷰가 필요하지

먼저 하나하나씩 리스트뷰에 추가 할 리스트뷰 레이아웃을 만든다.


이렇게 4개의 데이터를 집어넣는 레이아웃을 만들었어

이제 여기에 데이터를 하나하나씩 넣으면 이 모양대로 리스트뷰가 구성이되지.

데이터를 집어 넣어줄 class  파일을 생성해볼께

로 만들었고 코드는 이렇게 자기자신을 생성자로 받고 get set 을 받아오면 된다


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
package com.example.memoproz;
 
public class GetSet {
    int id;
    String name;
    String memo;
    String imageName;
    String day;
    
    
    public GetSet(int id, String name, String memo, String imageName, String day ) {
        this.id = id;
        this.name = name;
        this.memo = memo;
        this.imageName = imageName;
        this.day = day;
        // TODO Auto-generated constructor stub
    }
 
 
    public int getId() {
        return id;
    }
 
 
    public void setId(int id) {
        this.id = id;
    }
 
 
    public String getName() {
        return name;
    }
 
 
    public void setName(String name) {
        this.name = name;
    }
 
 
    public String getMemo() {
        return memo;
    }
 
 
    public void setMemo(String memo) {
        this.memo = memo;
    }
 
 
    public String getDay() {
        return day;
    }
 
 
    public void setDay(String day) {
        this.day = day;
    }
 
 
    public String getImageName() {
        return imageName;
    }
 
 
    public void setImageName(String imageName) {
        this.imageName = imageName;
    }
 
}
 
cs
나는 혹시나 따로 저장이 필요할까봐 이렇게 클래스를 구성했다

이제 이 데이터를을 리스트뷰에 올려야겠지? 이렇게 구성된 레이아웃을 올릴 어댑터가 필요해 어댑터는 리스트뷰와 리스트레이아웃을 연결시킬 연결고리라고 생각하면 돼

어댑터에도 종류가 많은데 나는 이번에 BaseAdapter을 사용할께

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
 
package com.example.memoproz;
 
import java.util.*;
 
import android.annotation.SuppressLint;
import android.content.*;
import android.graphics.*;
import android.view.*;
import android.widget.*;
 
@SuppressLint("ViewHolder")
public class CustomAdapter extends BaseAdapter{
    public Context context; // 컨텍스트 저장
    ArrayList<GetSet> MemoList; // 아이템 리스트
    
    public CustomAdapter(Context context, ArrayList<GetSet> MemoList){ //  생성자 생성
        this.context = context;
        this.MemoList = MemoList;
        
    } 
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return MemoList.size();
    }
 
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return MemoList.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) { // 가장중요한 곳 여기서 뷰생성
        // TODO Auto-generated method stub
        LayoutInflater inflater = null// 레이아웃 전개
        int row= 0;
        if(convertView == null){
            row = R.layout.customview;
        }
        convertView = inflater.inflate(row, parent, false);
        
        ImageView CustomMemoImg = (ImageView)convertView.findViewById(R.id.CustomImg);
        TextView CustomMemoName = (TextView)convertView.findViewById(R.id.CustomName);
        TextView CustomMemoMemo = (TextView)convertView.findViewById(R.id.CustomMemo);
        TextView CustomMemoDay = (TextView)convertView.findViewById(R.id.CustomDay);
        
        //CustomMemoImg.setImageBitmap(BitmapFactory.decodeFile(mPath));
        CustomMemoName.setText(MemoList.get(position).getName()); // 제목연결
        CustomMemoMemo.setText(MemoList.get(position).getMemo()); // 메모연결
        CustomMemoDay.setText(MemoList.get(position).getDay()); // 이름연결
        
        
        return convertView; // 뷰리턴
        
    }
 
}
 
cs
코드는 이런식이다 먼저 외부에서 사용할 생성자를 생성해놓고 getView에서 뷰들의 데이터를 읽어받아 데이터를 삽입하고 레이아웃으로 전개한다.

그리고는 이제 마지막으로 이 리스트뷰에 데이터를 집어넣어 실행시킬 메인뷰의 코드를 작성해보자.

메인뷰에서 나는 db로 받아와서 뿌려줬어

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
 
    public void viewDB(){
        String sql = "select * from " + TABLE_NAME;
        
        Cursor cursor = db.rawQuery(sql, null);
        
        if(cursor !=null){
            int count  = cursor.getCount();
            String countI = String.valueOf(count);
            Log.i("count", countI);
            for(int i=0; i<count; i++){
                cursor.moveToNext();
                int id = cursor.getInt(0);
                String name  = cursor.getString(1);
                String memo = cursor.getString(2);
                String day = cursor.getString(3);
                Log.i("name", name);
                //text.setText(id + name + memo + day);
                
                GetSet data = new GetSet(id, name, memo, "d", day);
                CustomMemoList.add(data);
                
                
            }
            
        }
        
    }
cs
add로 메모 리시트에 데이터를 삽입하였고

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memo_menu);
        
        //text = (TextView)findViewById(R.id.text1);
        db = openOrCreateDatabase(DB_NAME,MODE_WORLD_WRITEABLE,null);
        
        viewDB(); // 데이터 삽입
        
        
        ListView listView = (ListView)findViewById(R.id.List_row); // 리스트뷰 연결
        CustomAdapter customAdapter = new CustomAdapter(this, CustomMemoList);    // 커스텀 어댑터 생성자로 레이아웃 전개
        listView.setAdapter(customAdapter); // 리스트뷰에 삽입
        
        
        
    }
cs

메인 문에서는 리스트뷰로 연결하여서 삽입했어 
이렇게해서 실행을 하게되면 이제 이런식으로 원하는 리스트뷰를 정의해서 할수가 있다.


결과창이다 
이미지를 연동하는법은 다음장에서 설명하겠다.






728x90