Android适配器上下文

时间:2016-06-17 02:15:47

标签: java android adapter

Android开发新手并试图在我的应用程序适配器中获取Context但我不断获得null异常。我尝试将url字符串加载到glide中,但glide.with(context)不起作用。应用程序从sqlite中提取值(这部分工作正常),我现在尝试获取其中一个值(url)并通过滑行将其设置为imageview。 XML具有所需的所有视图。

错误:您无法在com.bumptech.glide.manager.RequestManagerRetriever.get

上启动对空上下文的加载

UsersAdapter.java

public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserViewHolder> {
private ArrayList<User> mDataSet;

public UsersAdapter(ArrayList<User> mDataSet) {
    this.mDataSet = mDataSet;
}

@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_favorites2, parent, false);
    UserViewHolder userViewHolder = new UserViewHolder(v);
    return userViewHolder;
}

@Override
public void onBindViewHolder(UserViewHolder holder, int position) {

    holder.name_entry.setText(mDataSet.get(position).getName());
    holder.email_entry.setText(mDataSet.get(position).getEmail());
    holder.icon_entry.setText(""+mDataSet.get(position).getName().charAt(0));

    String name = mDataSet.get(position).getName();
    String url = mDataSet.get(position).getEmail();

    // load image into imageview using glide
    Glide.with(context)
            .load(url)
            .asBitmap()
            .placeholder(R.drawable.ic_blank)
            .error(R.drawable.ic_blank)
            .into(holder.ivFavorites);
}

@Override
public int getItemCount() {
    return mDataSet.size();
}

public class UserViewHolder extends RecyclerView.ViewHolder {
    CardView cardView;
    TextView name_entry, email_entry, icon_entry;
    ImageView ivFavorites;

    UserViewHolder(View itemView) {
        super(itemView);
        cardView = (CardView) itemView.findViewById(R.id.user_layout);
        name_entry = (TextView) itemView.findViewById(R.id.name_entry);
        email_entry = (TextView) itemView.findViewById(R.id.email_entry);
        icon_entry = (TextView) itemView.findViewById(R.id.icon_entry);
        ivFavorites = (ImageView) itemView.findViewById(R.id.ivFavorites);
    }
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
}

阅读适配器和上下文我尝试了以下但但收效甚微。

public class UsersAdapter extends      RecyclerView.Adapter<UsersAdapter.UserViewHolder> {
private ArrayList<User> mDataSet;
private Context context;
public UsersAdapter(ArrayList<User> mDataSet) {
    this.mDataSet = mDataSet;
}
public UsersAdapter(ArrayList<User> mDataSet, Context context) {
    this.context = context;
}

我的 Favorites.java 活动,它从sqlite中提取值并在cardview中显示它们(这也很好,虽然我无法滑动加载图像):

公共类收藏夹扩展了AppCompatActivity {

private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private UsersAdapter adapter;
private DBHandler dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_favorites);
    recyclerView = (RecyclerView) findViewById(R.id.usersList);
    recyclerView.setHasFixedSize(true);
    layoutManager = new GridLayoutManager(this,2);
    recyclerView.setLayoutManager(layoutManager);
    dbHandler = new DBHandler(Favorites.this);

    Log.d("Reading: ", "reading contacts..");
    ArrayList<User> users = dbHandler.getAllUsers();
    adapter = new UsersAdapter(users);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

}

1 个答案:

答案 0 :(得分:0)

初始化适配器时传递上下文

adapter = new UsersAdapter(users,getApplicationContext());

并在您的适配器中,实现等效的构造函数

public UsersAdapter(ArrayList<User> mDataSet, Context context) {
    this.context = context;
    this.mDataSet = mDataSet;
}