351
No
class MainActivity
package com.example.choi.mystudy9;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<Weather> data=null;
private WeatherAdapter adapter=null;
private ListView listView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=new ArrayList<>();
data.add(new Weather("월", R.drawable.w_icon_01, "맑음"));
data.add(new Weather("화", R.drawable.w_icon_02, "흐림"));
data.add(new Weather("수", R.drawable.w_icon_03, "흐림/비"));
data.add(new Weather("목", R.drawable.w_icon_04, "비"));
data.add(new Weather("금", R.drawable.w_icon_01, "맑음"));
data.add(new Weather("토", R.drawable.w_icon_02, "흐림"));
data.add(new Weather("일", R.drawable.w_icon_03, "흐림/비"));
adapter=new WeatherAdapter(MainActivity.this, data);
listView=(ListView)findViewById(R.id.lv_weather);
listView.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.mystudy9.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Week Weather"
android:id="@+id/tv_title"
android:textSize="30sp"
android:fontFamily="sans-serif"
android:textStyle="normal|bold" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/tv_title"
android:id="@+id/lv_weather" />
</RelativeLayout>
class Weather
package com.example.choi.mystudy9;
/**
* Created by choi on 2017-03-03.
*/
public class Weather {
private String day;
private int icon;
private String comment;
public Weather(String day, int icon, String comment) {
this.day = day;
this.icon = icon;
this.comment = comment;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
@Override
public String toString() {
return "Weather{" +
"day='" + day + '\'' +
", icon=" + icon +
", comment='" + comment + '\'' +
'}';
}
}
class WeatherAdapter
package com.example.choi.mystudy9;
import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
/**
* Created by choi on 2017-03-03.
*/
public class WeatherAdapter extends BaseAdapter {
private Context mContext=null;
private ArrayList<Weather> data=null;
private LayoutInflater inflater=null;
private Layout layout;
private int pos;
public WeatherAdapter(Context context, ArrayList<Weather> data) {
this.mContext=context;
this.data=data;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position).getDay();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=inflater.inflate(R.layout.cusom_layout, parent, false);
}
TextView tv_day =(TextView)convertView.findViewById(R.id.tv_day);
ImageView iv_icon=(ImageView)convertView.findViewById(R.id.iv_weather);
TextView tv_comment=(TextView)convertView.findViewById(R.id.tv_comment);
Button bt_select=(Button)convertView.findViewById(R.id.bt_select);
tv_day.setText(data.get(position).getDay() + " | ");
iv_icon.setImageResource(data.get(position).getIcon());
tv_comment.setText(data.get(position).getComment());
if ((position%2)==1) {
convertView.setBackgroundColor(0x5000ff00);
}else{
convertView.setBackgroundColor(0x2000ff00);
}
bt_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "버튼", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
R.layout.cusom_layout
<?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="wrap_content"
android:weightSum="1">
<TextView
android:id="@+id/tv_day"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:paddingLeft="10dp"
/>
<ImageView
android:id="@+id/iv_weather"
android:layout_width="wrap_content"
android:layout_height="50dp"
/>
<TextView
android:id="@+id/tv_comment"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
/>
<Button
android:id="@+id/bt_select"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="Select"
android:background="@android:color/holo_orange_dark"
android:layout_weight="0.19"
android:layout_gravity="right"
android:layout_alignParentRight="true"
android:textColor="#ffffff"
android:layout_marginLeft="50dp" />
</LinearLayout>
댓글 ( 4)
댓글 남기기