我为应用创建了自定义列表适配器,但我不知道如何实现它。有人可以告诉我怎么样?我试图获取一个县的id并将其用作我的列表1,2,3中的项目编号,而不是0,1,2,3。
这是我的代码:
public class CustomAdapter : BaseAdapter<County>
{
Activity context;
public List<County> listCounty;
public CustomAdapter(Activity context, List<County> lCounties)
: base()
{
this.context = context;
this.listCounty = lCounties;
}
public override int Count {
get { return this.listCounty.Count; }
}
public override County this [int position] {
get { return this.listCounty[position]; }
}
public override View GetView (int position, View convertView, ViewGroup parent)
{
var item = this.listCounty[position];
var view = convertView;
if (convertView == null || !(convertView is LinearLayout))
view = context.LayoutInflater.Inflate (Resource.Layout.Main, parent, false);
var textTop = view.FindViewById (Resource.Id.listView) as TextView;
textTop.SetText (item.CountyName, TextView.BufferType.Normal);
return view;
}
public override long GetItemId (int position)
{
return position;
}
}
和他一起去的课程
public class County
{
public int ID { get; set; }
public string CountyName { get; set; }
}
答案 0 :(得分:0)
以下是自定义列表适配器的代码段
public class NavDrawerListAdapter extends BaseAdapter {
private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;
public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
this.context = context;
this.navDrawerItems = navDrawerItems;
}
@Override
public int getCount() {
return navDrawerItems.size();
}
@Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
TextView txtCount = (TextView) convertView.findViewById(R.id.counter);
imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
txtTitle.setText(navDrawerItems.get(position).getTitle());
// displaying count
// check whether it set visible or not
if(navDrawerItems.get(position).getCounterVisibility()){
txtCount.setText(navDrawerItems.get(position).getCount());
}else{
// hide the counter view
txtCount.setVisibility(View.GONE);
}
return convertView;
}
}