自定义列表视图部分 header
Custom listview section header
我有一个列表视图,其中填充了自定义数组适配器。我想添加部分 header 以按日期列出视图项目,这是 object 中的一个值。
我的适配器 class 是
public class ReceiptAdapter2 extends BaseAdapter {
Context context;
LayoutInflater inflater;
private ArrayList<ReceiptModel> planList;
public ReceiptAdapter2(Context applicationContext, ArrayList<ReceiptModel> planList) {
this.context = applicationContext;
this.planList = planList;
inflater = (LayoutInflater.from(applicationContext));
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (getItem(position) instanceof SectionItem){
return 0;
}else{
return 1;
}
}
@Override
public int getCount() {
return planList.size();
}
@Override
public Object getItem(int i) {
return planList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ReceiptModel model = planList.get(i);
view = inflater.inflate(R.layout.snipadapt, null);
TextView names = (TextView) view.findViewById(R.id.textView);
names.setText(model.getName());
return view;
}
}
MainActivity 代码
if (hstry.length() > 0 && hstry != null) {
for (int i = 0; i < hstry.length(); i++) {
try {
history_data = hstry.getJSONObject(i);
model = new ReceiptModel();
model_section = new SectionItem();
model.setRid(history_data.optString("ID"));
model.setName(history_data.optString("Name"));
model.setMobile(history_data.optString("Mobile"));
model.setTax(history_data.optString("Tax"));
model.setQnt(history_data.optString("T_quantity"));
model.setPrice(history_data.optString("T_price"));
model.setSub(history_data.optString("Sub_total"));
model.setDiscount(history_data.optString("Discount"));
model.setType(history_data.optString("Payment_type"));
model.setDate(db.getCatColor(history_data.optString("Date")));
model_section.setDevice(db.getCatColor(history_data.optString("Date")));
} catch (JSONException e) {
e.printStackTrace();
}
}
setAdapter();
}
设置适配器
private void setAdapter() {
adapter = new ReceiptAdapter2(context,planList);
category_list.setAdapter(adapter);
}
型号class
public class ReceiptModel {
private String id,name,mobile,tax,t_quantity,t_price,s_total,discount,date,type;
private String device;
private String os,location;
private String network,weather,storage,time,internal,cat_id;
public String getRid() {
return id;
}
public void setRid(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {return mobile; }
public void setMobile(String mobile) {this.mobile = mobile; }
public String getTax() {return tax; }
public void setTax(String tax) {this.tax = tax; }
public String getQnt() {return t_quantity; }
public void setQnt(String t_quantity) {this.t_quantity = t_quantity; }
public String getPrice() {return t_price; }
public void setPrice(String t_price) {this.t_price = t_price; }
public String getSub() {return s_total; }
public void setSub(String s_total) {this.s_total = s_total; }
public String getDiscount() {return discount; }
public void setDiscount(String discount) {this.discount = discount; }
public String getDate() {return date; }
public void setDate(String date) {this.date = date; }
}
我参考了很多教程,但没有任何效果 me.How 我可以将多个 arraylist 像我的情况一样传递给适配器 class 吗?感谢您的回复
您需要像这样将 ReceiptModel() 和 SelectionItem() 这两个模型添加为 ArrayList 的对象
private ArrayList<Object> lpAndAlp=new ArrayList<>();
lpAndAlp.add("model");
lpAndAlp.add("model_section");
然后在您的适配器中 lpAndAlp
private void setAdapter() {
adapter = new ReceiptAdapter2(context,planList);
category_list.setAdapter(adapter);
}
并像这样称呼它为您的适配器 class..
private ArrayList<Object> planList;
public ReceiptAdapter2(Context applicationContext, ArrayList<Object> planList) {
this.context = applicationContext;
this.planList = planList;
inflater = (LayoutInflater.from(applicationContext));
}
您只需检查 getView
中 getItemViewType
方法的 return 类型
@Override
public View getView ( int i, View view, ViewGroup viewGroup){
int type = getItemViewType(position);
if (view == null) {
switch (type) {
case 0:
view = inflater.inflate(R.layout.snipadapt, parent, false);
break;
case 1:
view = inflater.inflate(R.layout.yourHeaderLayout, parent, false);
break;
}
}
switch (type) {
case 0:
ReceiptModel model = planList.get(i);
view = inflater.inflate(R.layout.snipadapt, null);
TextView names = (TextView) view.findViewById(R.id.textView);
names.setText(model.getName());
break;
case 1:
SectionItem model_section = planList.get(i);
//your header
break;
}
return view;
}
在 ReceiptAdapter2 中
@Override
public int getItemViewType(int position) {
//CHECK IF CURRENT ITEM IS List THEN RETURN ROW
if(getItem(position) instanceof LISTSECTION)
{
return ROW;
}
//OTHERWISE RETURN HEADER
return HEADER;
}
在 getView 方法中:--- >
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//TYPE OF VIEW
int type=getItemViewType(pos);
//IF THERE IS NO VIEW CREATE IT
if(convertView==null)
{
inflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (type) {
case ROW:
convertView=inflater.inflate(R.layout.rowmodel, null);
break;
case HEADER:
convertView=inflater.inflate(R.layout.headermodel,null);
default:
break;
}
}
//OTHERWISE CHECK IF ITS ROW OR HEADER AND SET DATA ACCORDINGLY
switch (type) {
case ROW:
// Row data
break;
case HEADER:
//header data
default:
break;
}
return convertView;
}
我有一个列表视图,其中填充了自定义数组适配器。我想添加部分 header 以按日期列出视图项目,这是 object 中的一个值。 我的适配器 class 是
public class ReceiptAdapter2 extends BaseAdapter {
Context context;
LayoutInflater inflater;
private ArrayList<ReceiptModel> planList;
public ReceiptAdapter2(Context applicationContext, ArrayList<ReceiptModel> planList) {
this.context = applicationContext;
this.planList = planList;
inflater = (LayoutInflater.from(applicationContext));
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (getItem(position) instanceof SectionItem){
return 0;
}else{
return 1;
}
}
@Override
public int getCount() {
return planList.size();
}
@Override
public Object getItem(int i) {
return planList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ReceiptModel model = planList.get(i);
view = inflater.inflate(R.layout.snipadapt, null);
TextView names = (TextView) view.findViewById(R.id.textView);
names.setText(model.getName());
return view;
}
}
MainActivity 代码
if (hstry.length() > 0 && hstry != null) {
for (int i = 0; i < hstry.length(); i++) {
try {
history_data = hstry.getJSONObject(i);
model = new ReceiptModel();
model_section = new SectionItem();
model.setRid(history_data.optString("ID"));
model.setName(history_data.optString("Name"));
model.setMobile(history_data.optString("Mobile"));
model.setTax(history_data.optString("Tax"));
model.setQnt(history_data.optString("T_quantity"));
model.setPrice(history_data.optString("T_price"));
model.setSub(history_data.optString("Sub_total"));
model.setDiscount(history_data.optString("Discount"));
model.setType(history_data.optString("Payment_type"));
model.setDate(db.getCatColor(history_data.optString("Date")));
model_section.setDevice(db.getCatColor(history_data.optString("Date")));
} catch (JSONException e) {
e.printStackTrace();
}
}
setAdapter();
}
设置适配器
private void setAdapter() {
adapter = new ReceiptAdapter2(context,planList);
category_list.setAdapter(adapter);
}
型号class
public class ReceiptModel {
private String id,name,mobile,tax,t_quantity,t_price,s_total,discount,date,type;
private String device;
private String os,location;
private String network,weather,storage,time,internal,cat_id;
public String getRid() {
return id;
}
public void setRid(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {return mobile; }
public void setMobile(String mobile) {this.mobile = mobile; }
public String getTax() {return tax; }
public void setTax(String tax) {this.tax = tax; }
public String getQnt() {return t_quantity; }
public void setQnt(String t_quantity) {this.t_quantity = t_quantity; }
public String getPrice() {return t_price; }
public void setPrice(String t_price) {this.t_price = t_price; }
public String getSub() {return s_total; }
public void setSub(String s_total) {this.s_total = s_total; }
public String getDiscount() {return discount; }
public void setDiscount(String discount) {this.discount = discount; }
public String getDate() {return date; }
public void setDate(String date) {this.date = date; }
}
我参考了很多教程,但没有任何效果 me.How 我可以将多个 arraylist 像我的情况一样传递给适配器 class 吗?感谢您的回复
您需要像这样将 ReceiptModel() 和 SelectionItem() 这两个模型添加为 ArrayList 的对象
private ArrayList<Object> lpAndAlp=new ArrayList<>();
lpAndAlp.add("model");
lpAndAlp.add("model_section");
然后在您的适配器中 lpAndAlp
private void setAdapter() {
adapter = new ReceiptAdapter2(context,planList);
category_list.setAdapter(adapter);
}
并像这样称呼它为您的适配器 class..
private ArrayList<Object> planList;
public ReceiptAdapter2(Context applicationContext, ArrayList<Object> planList) {
this.context = applicationContext;
this.planList = planList;
inflater = (LayoutInflater.from(applicationContext));
}
您只需检查 getView
getItemViewType
方法的 return 类型
@Override
public View getView ( int i, View view, ViewGroup viewGroup){
int type = getItemViewType(position);
if (view == null) {
switch (type) {
case 0:
view = inflater.inflate(R.layout.snipadapt, parent, false);
break;
case 1:
view = inflater.inflate(R.layout.yourHeaderLayout, parent, false);
break;
}
}
switch (type) {
case 0:
ReceiptModel model = planList.get(i);
view = inflater.inflate(R.layout.snipadapt, null);
TextView names = (TextView) view.findViewById(R.id.textView);
names.setText(model.getName());
break;
case 1:
SectionItem model_section = planList.get(i);
//your header
break;
}
return view;
}
在 ReceiptAdapter2 中
@Override
public int getItemViewType(int position) {
//CHECK IF CURRENT ITEM IS List THEN RETURN ROW
if(getItem(position) instanceof LISTSECTION)
{
return ROW;
}
//OTHERWISE RETURN HEADER
return HEADER;
}
在 getView 方法中:--- >
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//TYPE OF VIEW
int type=getItemViewType(pos);
//IF THERE IS NO VIEW CREATE IT
if(convertView==null)
{
inflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (type) {
case ROW:
convertView=inflater.inflate(R.layout.rowmodel, null);
break;
case HEADER:
convertView=inflater.inflate(R.layout.headermodel,null);
default:
break;
}
}
//OTHERWISE CHECK IF ITS ROW OR HEADER AND SET DATA ACCORDINGLY
switch (type) {
case ROW:
// Row data
break;
case HEADER:
//header data
default:
break;
}
return convertView;
}