2012年8月17日 星期五

ViewHolder


Create a ViewHolder in the customized adapter.java


ListAdapter.java


public class ExampleAdapter extends BaseAdapter {

private ArrayList<HashMap<String, Object>> data;
private LayoutInflater inflater;

//Constructor
public ExampleAdapter(Context ctx, ArrayList<HashMap<String, Object>> d) {

inflater = LayoutInflater.from(ctx);
data = d;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

// @Override
// public int getItemViewType(int position) {
// // TODO Auto-generated method stub
// return 0;
// }

@Override
public View getView(int position, View view, ViewGroup parent) {

ViewHolder holder;

view = inflater.inflate(R.layout.row_list_rownull);
holder = new ViewHolder();
holder.icon = (ImageView)view.findViewById(R.id.icon);
holder.sportText = (TextView)view.findViewById(R.id.sportsname);
holder.noText = (TextView)view.findViewById(R.id.serialno);

holder.icon.setImageResource((Integer)data.get(position).get("icon"));
holder.sportText.setText((String)data.get(position).get("sport"));
holder.noText.setText((String)data.get(position).get("serialno"));
return view;
}

static class ViewHolder{
ImageView icon;
TextView sportText;
TextView noText;
}

}

沒有留言:

張貼留言