android adapter中怎么调用getresources
1个回答
展开全部
class FileAdapter extends BaseAdapter{
Context mContext;
LayoutInflater mLInflater;
public FileAdapter (Context context) {
this.mContext= context;
//mListFile=getFileData(path);
this.mLInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
if(mListFile.size()!=0&&mListFile!=null){
return mListFile.size();
}else{
return 0;
}
}
@Override
public Object getItem(int position) {
return mListFile.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null ;
if(convertView==null){
convertView = mLInflater.inflate(R.layout.sd_card_item, null);
holder = new ViewHolder();
holder.setFileName((TextView)convertView.findViewById(R.id.filename));
//holder.setmLastTime((TextView)convertView.findViewById(R.id.time));
holder.setImageIcon((ImageView)convertView.findViewById(R.id.image));
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
int id = 0;
String type = mListFile.get(position).getFileType();
if(type=="0"){
id = R.drawable.up1;
}else if(type=="1"){
id = R.drawable.sdcard0;
}else if(type=="2"){
id = R.drawable.folder2;
}else if(type=="3"){
id= R.drawable.other3;
}else if(type=="55"){
id=R.drawable.none;
}
holder.getFileName().setText(mListFile.get(position).getFileName());
holder.getImageIcon().setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), id));
holder.getmLastTime().setText(mListFile.get(position).getFileLastTime());
return convertView;
}
}
private class ViewHolder {
TextView fileName;
TextView mLastTime;
ImageView imageIcon;
public TextView getFileName() {
return fileName;
}
public void setFileName(TextView fileName) {
this.fileName = fileName;
}
public TextView getmLastTime() {
return mLastTime;
}
public void setmLastTime(TextView mLastTime) {
this.mLastTime = mLastTime;
}
public ImageView getImageIcon() {
return imageIcon;
}
public void setImageIcon(ImageView imageIcon) {
this.imageIcon = imageIcon;
}
}
代码中mListFile 为这个自定义的类型List;相信有上面部分还是相对来说比较好理解的!
Context mContext;
LayoutInflater mLInflater;
public FileAdapter (Context context) {
this.mContext= context;
//mListFile=getFileData(path);
this.mLInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
if(mListFile.size()!=0&&mListFile!=null){
return mListFile.size();
}else{
return 0;
}
}
@Override
public Object getItem(int position) {
return mListFile.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null ;
if(convertView==null){
convertView = mLInflater.inflate(R.layout.sd_card_item, null);
holder = new ViewHolder();
holder.setFileName((TextView)convertView.findViewById(R.id.filename));
//holder.setmLastTime((TextView)convertView.findViewById(R.id.time));
holder.setImageIcon((ImageView)convertView.findViewById(R.id.image));
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
int id = 0;
String type = mListFile.get(position).getFileType();
if(type=="0"){
id = R.drawable.up1;
}else if(type=="1"){
id = R.drawable.sdcard0;
}else if(type=="2"){
id = R.drawable.folder2;
}else if(type=="3"){
id= R.drawable.other3;
}else if(type=="55"){
id=R.drawable.none;
}
holder.getFileName().setText(mListFile.get(position).getFileName());
holder.getImageIcon().setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), id));
holder.getmLastTime().setText(mListFile.get(position).getFileLastTime());
return convertView;
}
}
private class ViewHolder {
TextView fileName;
TextView mLastTime;
ImageView imageIcon;
public TextView getFileName() {
return fileName;
}
public void setFileName(TextView fileName) {
this.fileName = fileName;
}
public TextView getmLastTime() {
return mLastTime;
}
public void setmLastTime(TextView mLastTime) {
this.mLastTime = mLastTime;
}
public ImageView getImageIcon() {
return imageIcon;
}
public void setImageIcon(ImageView imageIcon) {
this.imageIcon = imageIcon;
}
}
代码中mListFile 为这个自定义的类型List;相信有上面部分还是相对来说比较好理解的!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询