自定义adapter

首先,看activity对应的xml文件

 

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingTop="30dp"

    android:paddingLeft="10dp"

    android:paddingRight="10dp"

    android:background="#efe8e2"    

android:clipToPadding="false"  

android:fitsSystemWindows="true"  >

 

  <ListView android:id="@+id/date_list"

    android:dividerHeight="1dp"

    android:divider="#00000000"

        android:layout_width="fill_parent"  

        android:layout_height="fill_parent">

    </ListView>

   

</RelativeLayout>

 

在activity中

 

 

public class AddTimingChooseDateActivity extends Activity {

 

private static ListView listView;

public static Context context;

protected void onCreate(Bundle savedInstanceState){

String dates = null;

setContentView(R.layout.add_timing_date_list);//加载下xml

 

listView = (ListView)findViewById(R.id.date_list);//加载下xml中的listview

 

List<HashMap<String,Object>> list = getData(dates);

listView.setAdapter(new AddTimingDateAdapter(context,list));//AddTimingDateAdapter就是我自定义的adapter

super.onCreate(savedInstanceState);

}

//加载分解数据源(个人理解和叫法)

public static List<HashMap<String,Object>> getData(String retjson){

List<HashMap<String,Object>>list = new ArrayList<HashMap<String,Object>>();

 

    String[] dateArray = repeateDate.split("[ ]"); 

 

for(int i=0;i<7;i++){

 

HashMap<String,Object> map = new HashMap<String,Object>();

switch (i){

case 0:

map.put("date", "每周日");

if(Arrays.asList(dateArray).contains("周日")){

map.put("image", R.drawable.choose_date_selected);

}else{

map.put("image", R.drawable.choose_date_noselected);

}

break;

case 1:

map.put("date", "每周一");

if(Arrays.asList(dateArray).contains("周一")){

map.put("image", R.drawable.choose_date_selected);

}else{

map.put("image", R.drawable.choose_date_noselected);

}

break;

case 2:

map.put("date", "每周二");

if(Arrays.asList(dateArray).contains("周二")){

map.put("image", R.drawable.choose_date_selected);

}else{

map.put("image", R.drawable.choose_date_noselected);

}

break;

case 3:

map.put("date", "每周三");

if(Arrays.asList(dateArray).contains("周三")){

map.put("image", R.drawable.choose_date_selected);

}else{

map.put("image", R.drawable.choose_date_noselected);

}

break;

case 4:

map.put("date", "每周四");

if(Arrays.asList(dateArray).contains("周四")){

map.put("image", R.drawable.choose_date_selected);

}else{

map.put("image", R.drawable.choose_date_noselected);

AddTimingDateAdapter.rp[4] = 0;

}

break;

case 5:

map.put("date", "每周五");

if(Arrays.asList(dateArray).contains("周五")){

map.put("image", R.drawable.choose_date_selected);

 

}else{

map.put("image", R.drawable.choose_date_noselected);

}

break;

case 6:

map.put("date", "每周六");

if(Arrays.asList(dateArray).contains("周六")){

map.put("image", R.drawable.choose_date_selected);

 

}else{

map.put("image", R.drawable.choose_date_noselected);

}

break;

 

default:

break;

 

}

list.add(map);

}

 

return list;

}

 

}

 

现在开始看adapter对应的xml文件

<?xml version="1.0" encoding="utf-8"?>  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="fill_parent"

    android:layout_height="42dp" android:minHeight="42dp" android:background="#ffffff">

    

 

    <LinearLayout android:layout_width="match_parent"  

        android:layout_height="wrap_content"         

    android:layout_marginTop="0px"

        android:orientation="vertical">

    

    <LinearLayout android:orientation="horizontal"

        android:layout_width="match_parent"

        android:layout_height="40dp">

        

        <TextView android:id="@+id/choose_date"

            android:layout_width = "150dp"

            android:layout_height = "30dp"

            android:layout_marginLeft = "10dp"

            android:layout_marginTop = "15dp"

            android:textColor = "#666872"

            android:textSize = "30px"/>

        

      <ImageView android:id="@+id/choose_image_view"

       android:layout_width="30dp"

       android:layout_height="30dp"

       android:layout_marginTop = "5dp"

       android:background="@drawable/line"

      android:layout_marginLeft="110dp" />

        

    </LinearLayout>    

    

  </LinearLayout>

 

    <Button android:id="@+id/top_button"

        android:layout_width="match_parent" 

        android:layout_height="match_parent"

android:orientation="vertical"

android:background="#00FFFFFF"

>

    </Button>

    

    

  </RelativeLayout>

 

左边文字,右边图片,上边一个按钮

 

现在看adapter中的代码

 

public class AddTimingDateAdapter extends BaseAdapter {

 

public static List<HashMap<String,Object>>data;

private LayoutInflater layoutInflater;

private Context context;

public AddTimingDateAdapter(Context context, List<HashMap<String,Object>>data){

this.context = context;

this.data = data;

this.layoutInflater = layoutInflater.from(context);

}

//定义一个zujian来管理adapter中的控件和属性

public final class Zujian{

public ImageView chooseDateImage;

public TextView chooseDate;

public Button topButton;

public Boolean mode = false;

public int position;

}

 

 

@Override

public int getCount() {

// TODO Auto-generated method stub

return data.size();

}

 

@Override

public Object getItem(int arg0) {

// TODO Auto-generated method stub

return data.get(arg0);

}

 

@Override

public long getItemId(int arg0) {

// TODO Auto-generated method stub

return arg0;

}

 

@Override

public View getView(int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

Zujian zujian = null;

 

if(convertView == null){

 

zujian = new Zujian();

//根据id加载控件

convertView = layoutInflater.from(context).inflate(R.layout.add_timing_date, null);

zujian.chooseDate = (TextView)convertView.findViewById(R.id.choose_date);

zujian.chooseDateImage = (ImageView)convertView.findViewById(R.id.choose_image_view);

zujian.topButton = (Button)convertView.findViewById(R.id.top_button);

}

//给控件负值操作

zujian.chooseDate.setText((String) data.get(position).get("date"));

zujian.chooseDateImage.setBackgroundResource((Integer)data.get(position).get("image"));

zujian.position = position;

 

zujian.topButton.setTag(zujian);

zujian.topButton.setOnClickListener(new View.OnClickListener() {

 

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

Zujian zj = (Zujian) (arg0.getTag());

 

if(zj.mode){

zj.mode = false;

zj.chooseDateImage.setImageResource(R.drawable.choose_date_noselected);

rp[zj.position] = 0;

}else{

zj.mode = true;

zj.chooseDateImage.setImageResource(R.drawable.choose_date_selected);

rp[zj.position] = 1;

}

 

}

});

return convertView;

}

 

}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值