我试图在Android应用程序中将onlclick侦听器设置为recycleview,但是尽管一切正确,但我在IDE中仍然遇到上述错误。
这是他的DrinksActivity类的完整代码,显示了我如何尝试添加clickListener
public class DrinksActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnItemClickListener {
//recyclerview object
private RecyclerView recyclerView;
//adapter object
private RecyclerView.Adapter adapter;
private ItemsAdapter itemsAdapter;
//database reference
private DatabaseReference mDatabase;
//progress dialog
private ProgressDialog progressDialog;
//list to hold all the uploaded images
private List<Upload> uploads;
TableLayout item;
Menu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drinks);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
item = findViewById(R.id.item);
progressDialog = new ProgressDialog(this);
uploads = new ArrayList<>();
//displaying progress dialog while fetching images
progressDialog.setMessage("Loading Items Please wait...");
progressDialog.show();
mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);
Query query = mDatabase.orderByChild("category").equalTo("Ecohim");
//adding an event listener to fetch values
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
//dismissing the progress dialog
progressDialog.dismiss();
//iterating through all the values in database
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
uploads.add(upload);
}
//creating adapter
adapter = new ItemsAdapter(getApplicationContext(), uploads);
//adding adapter to recyclerview
recyclerView.setAdapter(adapter);
adapter.setClickListener(ItemsAdapter.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem itemCart = menu.findItem(R.id.action_settings);
LayerDrawable icon = (LayerDrawable) itemCart.getIcon();
setBadgeCount(this, icon, "9");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
else if(id == R.id.action_check_out){
}
else if(id == R.id.action_clear_cart){
MenuItem itemCart = menu.findItem(R.id.action_settings);
LayerDrawable icon = (LayerDrawable) itemCart.getIcon();
setBadgeCount(this, icon, "9");
}
else{
startActivity(new Intent(this, LoginActivity.class));
finish();
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_manage) {
} else if (id == R.id.nav_drinks) {
startActivity(new Intent(this, DrinksActivity.class));
} else if (id == R.id.nav_seeds) {
startActivity(new Intent(this, DrinksActivity.class));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public static void setBadgeCount(Context context, LayerDrawable icon, String count) {
BadgeDrawable badge;
// Reuse drawable if possible
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
if (reuse != null && reuse instanceof BadgeDrawable) {
badge = (BadgeDrawable) reuse;
} else {
badge = new BadgeDrawable(context);
}
badge.setCount(count);
icon.mutate();
icon.setDrawableByLayerId(R.id.ic_badge, badge);
}
@Override
public void onClick(View view, int position) {
Toast.makeText(getApplicationContext(), "Enter Your Email",Toast.LENGTH_SHORT).show();
}
}
这是ItemsAdapter类文件,在其中找到setClickListener方法
public class ItemsAdapter extends RecyclerView.Adapter<ItemsAdapter.ViewHolder> {
private Context context;
private List<Upload> uploads;
private OnItemClickListener clickListener;
public ItemsAdapter(Context context, List<Upload> uploads) {
this.uploads = uploads;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_items, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Upload upload = uploads.get(position);
holder.textViewName.setText(upload.getName());
holder.textdescription.setText(upload.getDescription());
holder.textcost.setText( "Ksh" + upload.getCost());
Glide.with(context).load(upload.getUrl()).into(holder.imageView);
}
@Override
public int getItemCount() {
return uploads.size();
}
public void setClickListener(OnItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textViewName, textdescription, textcost;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.textViewName);
textdescription = itemView.findViewById(R.id.textdescription);
textcost = itemView.findViewById(R.id.textcost);
imageView = itemView.findViewById(R.id.imageView);
itemView.setOnClickListener(this); // bind the listener
}
@Override
public void onClick(View v) {
clickListener.onClick(v, getPosition()); // call the onClick in the OnItemClickListener
}
}
}
最后,这是OnItemClickListener接口代码
public interface OnItemClickListener {
void onClick(View view, int position);}
我删除了所有导入和程序包声明行,以使代码更短。 我可能在做错什么,如何解决? 谢谢
答案 0 :(得分:4)
您即将解决问题。您在活动方面做得很好。您的思维方式是正确的。
问题是您将onItemClickedListener转换为OnClickListener的方式。
首先,您应该重命名(通过重构)您的自定义ViewHolder,因为它可能导致混乱。 (可选)
public class ViewHolder extends RecyclerView.ViewHolder
重命名为:
public class CustomViewHolder extends RecyclerView.ViewHolder
接下来,不要直接实现onClickListener,只需使用setter即可:
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName, textdescription, textcost;
public ImageView imageView;
//save itemview
private View itemView;
public void ViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.textViewName);
textdescription = itemView.findViewById(R.id.textdescription);
textcost = itemView.findViewById(R.id.textcost);
//assign itemView
this.itemView = itemView;
imageView = itemView.findViewById(R.id.imageView);
//don't set listener here
}
//create a setter method
public setHolderClickListener(View.OnClickListener listener){
itemView.setOnClickListener(listener);
}
}
最后,我们将使用一个简单的闭包将位置转移到onItemClickListener:
public class ItemsAdapter extends RecyclerView.Adapter<ItemsAdapter.CustomViewHolder> {
...
@Override
//I used renamed ViewHolder
public void onBindViewHolder(CustomViewHolder holder, int position) {
Upload upload = uploads.get(position);
holder.textViewName.setText(upload.getName());
holder.textdescription.setText(upload.getDescription());
holder.textcost.setText( "Ksh" + upload.getCost());
Glide.with(context).load(upload.getUrl()).into(holder.imageView);
//assign listener here:
holder.setHolderClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//this is the place where you should call this and everything works ;)
clickListener.onClick(v,position);
}
};
}
}
最终的适配器类:
public class ItemsAdapter extends RecyclerView.Adapter<ItemsAdapter.CustomViewHolder> {
private Context context;
private List<Upload> uploads;
private OnItemClickListener clickListener;
public ItemsAdapter(Context context, List<Upload> uploads) {
this.uploads = uploads;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_items, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
Upload upload = uploads.get(position);
holder.textViewName.setText(upload.getName());
holder.textdescription.setText(upload.getDescription());
holder.textcost.setText( "Ksh" + upload.getCost());
Glide.with(context).load(upload.getUrl()).into(holder.imageView);
//assign listener here:
holder.setHolderClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//this is the place where you should call this and everything works ;)
clickListener.onClick(v,position);
}
};
}
@Override
public int getItemCount() {
return uploads.size();
}
public void setClickListener(OnItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
public class CustomViewHolder extends RecyclerView.ViewHolder{
public TextView textViewName, textdescription, textcost;
public ImageView imageView;
//save itemview
private View itemView;
public void CustomViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.textViewName);
textdescription = itemView.findViewById(R.id.textdescription);
textcost = itemView.findViewById(R.id.textcost);
//assign itemView
this.itemView = itemView;
imageView = itemView.findViewById(R.id.imageView);
//don't set listener here
}
//create a setter method
public void setHolderClickListener(View.OnClickListener listener){
itemView.setOnClickListener(listener);
}
}
}
重要:请确保导入您的OnItemClickListener
NB :这只是解决问题的一种方法。您还可以使用lambda表达式,自定义接口,事件总线或反应式流。希望它能对您有所帮助:D
答案 1 :(得分:2)
将DrinkActivity
中适配器的声明更改为
private ItemsAdapter adapter;
我不知道您为什么声明了两个适配器,一个应该这么做。
,然后通过此适配器调用setClickListener
adapter.setClickListener(DrinkActivity.this);
OR
编辑adapter.setClickListener
行到此
((ItemsAdapter)adapter).setClickListener(DrinkActivity.this)
答案 2 :(得分:1)
尝试将您的itemsAdapter类代码更改为此
public class ItemsAdapter extends RecyclerView.Adapter<ItemsAdapter.ViewHolder> {
private static final String TAG = "RecyclerViewAdapter";
private Context context;
private List<Upload> uploads;
Menu menu;
private DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);
public ItemsAdapter(Context context, List<Upload> uploads) {
this.uploads = uploads;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_items, parent, false);
ViewHolder holder = new ViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
final Upload upload = uploads.get(position);
holder.textViewName.setText(upload.getName());
holder.textdescription.setText(upload.getDescription());
holder.textcost.setText( "Ksh" + upload.getCost());
holder.buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked on: " + uploads.get(position));
//add what you want done when the button clicked here
});
Glide.with(context).load(upload.getUrl()).into(holder.imageView);
}
@Override
public int getItemCount() {
return uploads.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView textViewName, textdescription, textcost;
public ImageView imageView;
public Button buy;
public ViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.textViewName);
textdescription = itemView.findViewById(R.id.textdescription);
textcost = itemView.findViewById(R.id.textcost);
imageView = itemView.findViewById(R.id.imageView);
buy = itemView.findViewById(R.id.addToCart);
}
}
}
那应该让您排序。 请注意,我已经将onclick侦听器添加到了DrinksActivity类中的addToCart按钮中。