안드로이드 스튜디오 CustomViewList2
안드로이드 스튜디오반응형
|
모델 클래스 작성 순서 1. 클래스 생성한다 2. 필요한 멤버 변수를 선언 3. 생성자를 만든다 4. 멤버 변수들은 getter() /setter()를 만든다 5. 필요시 toString(), equals(), hashcode() 을 재정의한다 |
|
커스텀 어댑터 작성 순서 1. BaseAdapter를 상속 받는 클래스 생성 2. 네 가지 오버라이드 메서드를 구현 3. 필요시 생성자 또는 public 메서드 등을 추가하여 데이터를 받을 수 있도록 한다 |

/* 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="wrap_content"
>
<ImageView
android:id="@+id/weather_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/city_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="도시명"
android:textSize="30dp" />
<TextView
android:id="@+id/temp_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="end"
android:text="기온" />
</LinearLayout
>
</LinearLayout
>
/* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/item" />
</LinearLayout>
/* Weather.java */
package com.example.adapter;
public class Weather {
private String city;
private String temp;
private String weather;
public Weather(String city, String temp, String weather) {
this.city = city;
this.temp = temp;
this.weather = weather;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
@Override
public String toString() {
return "Weather{" + "city='" + city + '\'' + ", temp='" + temp + '\'' + ", weather='" + weather + '\'' + '}';
}
}
/* MyAdapter.java */
package com.example.adapter;
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.HashMap;
import java.util.List;
import java.util.Map;
public class MyAdapter extends BaseAdapter {
private List<Weather> mydata;
private Map<String, Integer> mWeatherImageMap;
public MyAdapter(List<Weather> mData) {
this.mydata = mData;
mWeatherImageMap = new HashMap<>();
mWeatherImageMap.put("맑음", R.drawable.sunny);
mWeatherImageMap.put("폭설", R.drawable.blizzard);
mWeatherImageMap.put("구름", R.drawable.cloudy);
mWeatherImageMap.put("비", R.drawable.rainy);
mWeatherImageMap.put("눈", R.drawable.snow);
}
@Override
public int getCount() {
return mydata.size();
}
@Override
public Object getItem(int position) {
return mydata.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null ){
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
ImageView weather_image = (ImageView) convertView.findViewById(R.id.weather_image);
TextView city_text = (TextView) convertView.findViewById(R.id.city_text);
TextView temp_text = (TextView) convertView.findViewById(R.id.temp_text);
holder = new ViewHolder();
holder.weather = weather_image;
holder.city = city_text;
holder.temp = temp_text;
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
Weather weather = mydata.get(position);
holder.city.setText(weather.getCity());
holder.temp.setText(weather.getTemp());
holder.weather.setImageResource(mWeatherImageMap.get(weather.getWeather()));
return convertView;
}
static class ViewHolder{
ImageView weather;
TextView city;
TextView temp;
}
}
package com.example.adapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Weather> data = new ArrayList<>();
data.add(new Weather("서울","-5","폭설"));
data.add(new Weather("부산","3","눈"));
data.add(new Weather("대구","30","맑음"));
data.add(new Weather("광주","15","구름"));
data.add(new Weather("울산","25","맑음"));
data.add(new Weather("인천","18","맑음"));
data.add(new Weather("제주도","9","구름"));
MyAdapter adapter = new MyAdapter(data);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}

(activity_main.xml과 Mainactivity.java에서 Listview를 GridView로 바꿀 수 있다)

반응형
'안드로이드 스튜디오' 카테고리의 다른 글
| 안드로이드 스튜디오 데이터 저장하기 (SharedPreferences) (0) | 2020.05.03 |
|---|---|
| 안드로이드 스튜디오 GridView (0) | 2020.05.03 |
| 안드로이드 스튜디오 CustomListView (0) | 2020.05.02 |
| 안드로이드 스튜디오 ListView 3 (추가, 삭제, 수정) (0) | 2020.05.02 |
| 안드로이드 스튜디오 ListView 2 (이벤트 처리) (0) | 2020.05.02 |