我想将ImageView组件中的可绘制图像作为参数,所以我可以在其他类中更改它。这是我的主类代码:
private void StartData(ArrayList<Map<String, Object>> List, int Resource) {
this.Adapter = new BasicAdapter2(getActivity(), List, Resource,
new String[] { "Provider", "ProdName", "NoType", "ContractNo",
"AcceptTime", "ContractStatus", "Premium", "Gain",
"ReceivedPremium", "Name" },
new int[] {R.id.contractitem_label_Provider,
R.id.contractitem_label_ProdName,
R.id.contract_label_cno,
R.id.contractitem_label_contractNo,
R.id.contractitem_label_acceptTime,
R.id.contractitem_label_contractStatus,
R.id.contractitem_label_premium,
R.id.contractitem_label_feilv,
R.id.contractitem_label_receivedPremium,
R.id.contractitem_label_Name },
new int[] {R.id.listitem_button_look, R.id.listitem_button_pay,
R.id.listitem_main1,R.id.contractitem_label_companyImage,R.id.contractitem_label_payImage },
new BasicAdapter2.ListAdapterListener() {
@Override
public void onClickAtOKButton(View v) {
String company = dTable.GetValue((Integer)v.getTag(), 1).toString();
switch (v.getId()) {
case R.id.listitem_button_pay:
if (company.contains("xx")) {
Toast.makeText(getActivity(),"xxxxx", Toast.LENGTH_SHORT).show();
return;
}
String polnum = dTable.GetValue((Integer)v.getTag(), 3).toString();
Intent intent = new Intent();
intent.setClass(getActivity(),PolicyInformationActivity.class);
intent.putExtra("polnum", polnum);
startActivity(intent);
break;
case R.id.listitem_button_look:
Toast.makeText(getActivity(),"cccc...", Toast.LENGTH_SHORT).show();
break;
case R.id.listitem_main1:
break;
}
}
},
new BasicAdapter2.VisibleAdapterListener() {
@Override
public int onSetVisible(View v) {
String company = dTable.GetValue((Integer)v.getTag(), 1).toString();
int drawId;
if (company.contains("xx")) {
drawId = getResources().getInteger(R.drawable.taikang2x);
LogUtil.e("tai=========", drawId+"1");
return drawId;
} else if (company.contains("yy")) {
drawId = getResources().getInteger(R.drawable.youban2x);
LogUtil.e("you=========", drawId+"1");
return drawId;
} else {
return 0;
}
}
});
handler = new Handler();
this.Adapter.Start(this.MainView);
this.Start += Step;
this.End += Step;
this.Adapter.notifyDataSetChanged();
}
适配器:
public class BasicAdapter2 extends SimpleAdapter {
Context context;
private ArrayList<Integer> Indexs;
private ListAdapterListener mListener;
private VisibleAdapterListener vListener;
public interface ListAdapterListener { // create an interface
void onClickAtOKButton(View v); // create callback function
}
public interface VisibleAdapterListener { // create an interface
int onSetVisible(View v); // create callback function
}
public BasicAdapter2(Context context,List<? extends Map<String,?>> data,
int resource,String[] from,int[] to,ListAdapterListener mListener){
super(context,data,resource,from,to);
this.context=context;
this.Indexs =new ArrayList<Integer>();
this.mListener = mListener;
}
public BasicAdapter2(Context context,List<? extends Map<String,?>> data,
int resource,String[] from,int[] to,int[] Ids,
ListAdapterListener mListener,VisibleAdapterListener vListener){
super(context,data,resource,from,to);
this.context=context;
this.Indexs =new ArrayList<Integer>();
this.mListener = mListener;
if (Ids != null) {
for (int i = 0; i < Ids.length; i++) {
this.Indexs.add(Ids[i]);
}
}
}
public void Add(Integer ViewId){this.Indexs.add(ViewId);}
@Override
public View getView(final int position,View convertView,ViewGroup parent){
View view = super.getView(position, convertView, parent);
for(int i=0;i<this.Indexs.size();i++){
final View TempView=(View) view.findViewById(this.Indexs.get(i));
TempView.setOnClickListener(new View .OnClickListener() {
@Override
public void onClick(View v) {
mListener.onClickAtOKButton(TempView);
}
});
TempView.setTag(position);
TempView.setBackgroundResource(vListener.onSetVisible(TempView));
}
return view;
}
public void Start(ListView View){
View.setAdapter(this);
this.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(View view,Object data,String textRepresentation) {
if(view instanceof TextView && data instanceof HkBoolean){
HkBoolean Data = (HkBoolean) data;
view.setVisibility(Data.GetVisibility());
return true;
}else if(view instanceof ImageView && data instanceof Bitmap){
ImageView iv = (ImageView) view;
iv.setImageBitmap((Bitmap) data);
return true;
}else if(view instanceof ImageView && data instanceof Integer){
ImageView iv = (ImageView) view;
iv.setBackgroundResource((Integer) data);
return true;
}else {
return false;
}
}
});
}
}
在我的主要课程中,我尝试了#34; drawId = getResources()。getInteger(R.drawable.taikang2x);&#34;和&#34; drawId = R.drawable.taikang2x;&#34; ,但是&#34; NullPointerException&#34;。出了什么问题?