嗨,我是Android开发新手。我已经创建了一个应用程序,其中图片现在显示在gridview中。但我想让它更加用户友好。我找到了一个例子,但我需要一些帮助来实现我的代码作为示例。我想将我的UserList.java编写为ImageAdapter.java。我想使用imageview而不是holder。我怎么能这样做?
我正试图遵循的例子:
ImageAdapter.java
package com.step2rock.www.photographynowroadtopro;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
/**
* Created by Sushimz on 5/7/2016.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
// R.drawable.pic_7, R.drawable.pic_8,
// R.drawable.pic_9, R.drawable.pic_10,
// R.drawable.pic_11, R.drawable.pic_12,
// R.drawable.pic_13, R.drawable.pic_14,
// R.drawable.pic_15
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
现在这是我的代码。
UserList.java
package com.step2rock.www.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.step2rock.www.crudproject.R;
import com.step2rock.www.crudproject.UserlistActivity;
import com.step2rock.www.model.User;
public class UserList extends BaseAdapter {
private Context mContext;
LayoutInflater inflater;
UserlistActivity activity;
ArrayList<User> users;
public UserList(Context context, ArrayList<User> users) {
mContext = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.users = users;
}
@Override
public int getCount() {
return users.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
static class ViewHolder {
public ImageView ivUserImage;
public TextView tvUserName;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
activity = new UserlistActivity();
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.userrow_activity, null);
holder.ivUserImage = (ImageView) convertView.findViewById(R.id.ivUserImage);
holder.tvUserName = (TextView) convertView.findViewById(R.id.tvHeader);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
User user = new User();
user = users.get(position);
holder.ivUserImage.setImageBitmap(convertToBitmap(user.get_user_pic()));
holder.tvUserName.setText(user.get_first_name());
return convertView;
}
public Bitmap convertToBitmap(String base64String) {
byte[] decodedString = Base64.decode(base64String, Base64.DEFAULT);
Bitmap bitmapResult = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return bitmapResult;
}
}