这里是针对开源的下拉刷新插件(PullToRefreshListView)
主要有两步
1.在适配器里给指定的 控件对象设定 setTag(Object);
2.在使用这个适配器的Acvtivity 里找到之前设定的对象 然后getTag();
例如:
第一步:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHoler vh = null;
if (convertView == null) {
vh = new ViewHoler();
convertView = inflater.inflate(R.layout.search_sub_unit_items, null);
vh.companyname = (TextView) convertView.findViewById(R.id.tv_lv_companyname);//获得这个对象 注意这里
vh.manager = (TextView) convertView.findViewById(R.id.tv_lv_manager);
convertView.setTag(vh);
} else {
vh = (ViewHoler) convertView.getTag();
}
ChoicebyWork choicebyWork = choicebyWorkList.get(position);
vh.companyname.setText(choicebyWork.getSubcontractName());
vh.companyname.setTag(choicebyWork);//这里setTag(Object) 把DTO放进去 注意这里
vh.manager.setText(choicebyWork.getManagerName());
return convertView;
}
第二步:在listview的点击监控事件 OnItemClickListener 里通过view.getTag()或 findViewById(R.id.控件的ID) 获得当前点击的控件 getTag()强转成对应对象
具体看我的逻辑
actualListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 点击头部、底部栏无效
if (position == 0 || view == listView_footer)
return;
Hiddendantype hiddentypeData=null;
if (view instanceof TextView) {
hiddentypeData = (Hiddendantype)view.getTag();//注意这里
}
TextView tv = (TextView) view.findViewById(R.id.hiddend_projectList_categoryname);//注意这里
if(tv!=null){
hiddentypeData = (Hiddendantype)tv.getTag();//注意这里
}
if (hiddentypeData == null) {
return;
}
//selectprojectname.setText(hiddentypeData.getCategoryname());
}
});