본문 바로가기
Android

ListView

by 일용직 코딩노동자 2019. 9. 10.
728x90
반응형

-제가 기억하기 위한 공간으로 포스트하는 곳으로 쓰입니다-

 

activity_main.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="vertical">

<ListView
android:id="@+id/List_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>

 

 

ListViewAdapter.java

import android.content.Context;
import android.graphics.drawable.Drawable;
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 android.widget.Toast;


import com.example.yujin.listview.ListVO.ListVO;
import com.example.yujin.listview.R;

import java.util.ArrayList;

public class ListViewAdapter extends BaseAdapter {

private ArrayList<ListVO> listVO = new ArrayList<ListVO>() ;
public ListViewAdapter() {
}

@Override
public int getCount() {
return listVO.size() ;
}

// ** 이 부분에서 리스트뷰에 데이터를 넣어줌 **
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//postion = ListView의 위치 / 첫번째면 position = 0
final int pos = position;
final Context context = parent.getContext();

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_listview, parent, false);
}

ImageView image = (ImageView) convertView.findViewById(R.id.img) ;
TextView title = (TextView) convertView.findViewById(R.id.title) ;
TextView Context = (TextView) convertView.findViewById(R.id.context) ;

ListVO listViewItem = listVO.get(position);

// 아이템 내 각 위젯에 데이터 반영
image.setImageDrawable(listViewItem.getImg());
title.setText(listViewItem.getTitle());
Context.setText(listViewItem.getContext());

//리스트뷰 클릭 이벤트
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, (pos+1)+"번째 리스트가 클릭되었습니다.", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}

@Override
public long getItemId(int position) {
return position ;
}

@Override
public Object getItem(int position) {
return listVO.get(position) ;
}

// 데이터값 넣어줌
public void addVO(Drawable icon, String title, String desc) {
ListVO item = new ListVO();

item.setImg(icon);
item.setTitle(title);
item.setContext(desc);

listVO.add(item);
}
}

ListVO.java

import android.graphics.drawable.Drawable;

public class ListVO {
private Drawable img;
private String Title;
private String context;

public Drawable getImg() {
return img;
}

public void setImg(Drawable img) {
this.img = img;
}

public String getTitle() {
return Title;
}

public void setTitle(String title) {
Title = title;
}

public String getContext() {
return context;
}

public void setContext(String context) {
this.context = context;
}
}

 

 

MainActivity.java

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.widget.ListView;

import com.example.yujin.listview.Adapter.ListViewAdapter;


public class MainActivity extends Activity {

private ListView listview ;
private ListViewAdapter adapter;
private int[] img = {R.drawable.k2,R.drawable.green,R.drawable.foot};
private String[] Title = {"유리의 성","준비 없는 이별","발걸음"};
private String[] Context = {"K2 김성면","녹색지대","에메랄드 캐슬"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//변수 초기화
adapter = new ListViewAdapter();
listview = (ListView) findViewById(R.id.List_view);

//어뎁터 할당
listview.setAdapter(adapter);

//adapter를 통한 값 전달
for(int i=0; i<img.length;i++){
adapter.addVO(ContextCompat.getDrawable(this,img[i]),Title[i],Context[i]);
}
}
}

 

--결과--

 

궁금하신 사항은 댓글 남겨주세요.

728x90
반응형

'Android' 카테고리의 다른 글

SharedPreferences (간단한 정보저장)  (0) 2019.09.10
비콘감지  (11) 2019.09.10
EditText/TextView/ImageView/Button/Radio/RadioGroup/URI/Intent  (0) 2019.09.10
Image ViewPager  (11) 2019.09.10
ViewPager  (0) 2019.09.10

댓글