«

BaseAdapter数据刷新,自定义BaseAdapter

时间:2024-3-2 17:32     作者:韩俊     分类: Android


在网上的视频看到的一个很好用的自定义BaseAdapter,自己修改了一下后一直在用,分享给大家,这样的话绑定gridView或listView就方便多了,直接在adapter里面初始化数据源,十分的方便,希望对大家有用,有可以改进的地方大家一起讨论

在自定义的BaseAdapter中绑定数据源,数据源为泛型

代码:

文件名:McxBaseAdapter.java

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;


/**
* Adapter基本类
*
* @project NewSilkRoad
* @author MCX
* @date 2015-4-13
* @time 下午2:02:44
* @param <T>
*/
public abstract class McxBaseAdapter<T> extends BaseAdapter {


public McxBaseAdapter(Context context, int resid) {
this.context = context;
listCellId = resid;
}


private Context context;
private int listCellId = 0;


public Context getContext() {
return context;
}


// 数据源
private List<T> myList = new ArrayList<T>();


public void add(T item) {
myList.add(item);
}


public void remove(int position) {
myList.remove(position);
}


public void removeLast() {
remove(myList.size() - 1);
}


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


@Override
public T getItem(int position) {
return myList.get(position);
}


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


@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(listCellId,
null);
}
initListCell(position, convertView, parent);
return convertView;
}


protected abstract void initListCell(int position, View listCell,
ViewGroup parent);
}


使用方法:

activity中:

private GridView gv_group_tour;

private McxBaseAdapter<GroupTour> adapter; //GroupTour为自定义的数据类型

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_group_tour);


initView();//初始化方法


initEvent();//初始化事件


}


private void initEvent() {

//绑定adapter
gv_group_tour.setAdapter(adapter);

//添加数据,根据自定义的model添加数据,也可改成解析后的数据源,然后进行匹配即可
adapter.add(new GroupTour("aa", "bb", "1000", "1000",
"9999-01-01至12-12", R.drawable.im_ztydatu1,
R.drawable.btn_segeren));

}


private void initView() {


gv_group_tour = (GridView) findViewById(R.id.gv_group_tour);

//初始化自定义的McxBaseAdapter,R.layout.activity_group_tour_gv_cell为gridview的item样式
adapter = new McxBaseAdapter<GroupTour>(GroupTourActivity.this,
R.layout.activity_group_tour_gv_cell) {


@Override
protected void initListCell(int position, View listCell,
ViewGroup parent) {

//初始化数据源
GroupTour data = getItem(position);

//初始化activity_group_tour_gv_cell中的控件,并从数据源中添加数据,下面同理
LinearLayout ly_img = (LinearLayout) listCell
.findViewById(R.id.ly_group_tour_img);
ly_img.setBackgroundResource(data.getImg());


ImageView img_head = (ImageView) listCell
.findViewById(R.id.img_group_tour_headimg);
img_head.setBackgroundResource(data.getHeadimg());


TextView tv_title = (TextView) listCell
.findViewById(R.id.tv_group_tour_title);
tv_title.setText(data.getTitle());


TextView tv_username = (TextView) listCell
.findViewById(R.id.tv_group_tour_username);
tv_username.setText(data.getUsername());


TextView tv_number = (TextView) listCell
.findViewById(R.id.tv_group_tour_number);
tv_number.setText(data.getNumber());


TextView tv_money = (TextView) listCell
.findViewById(R.id.tv_group_tour_money);
tv_money.setText(data.getMoney());


TextView tv_time = (TextView) listCell
.findViewById(R.id.tv_group_tour_time);
tv_time.setText(data.getTime());
}
};


}

自定义的model:

文件名:GroupTour.java

public class GroupTour {


public GroupTour(String title, String username, String number,
String money, String time, int img, int headimg) {

this.title = title;
this.username = username;
this.number = number;
this.money = money;
this.time = time;
this.img = img;
this.headimg = headimg;

}


private String title;
private String username;
private String number;
private String money;
private String time;
private int img;
private int headimg;


public int getHeadimg() {
return headimg;
}


public void setHeadimg(int headimg) {
this.headimg = headimg;
}


public String getTitle() {
return title;
}


public void setTitle(String title) {
this.title = title;
}


public String getUsername() {
return username;
}


public void setUsername(String username) {
this.username = username;
}


public String getNumber() {
return number;
}


public void setNumber(String number) {
this.number = number;
}


public String getMoney() {
return money;
}


public void setMoney(String money) {
this.money = money;
}


public String getTime() {
return time;
}


public void setTime(String time) {
this.time = time;
}


public int getImg() {
return img;
}


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


}


标签: android

热门推荐