gpt4 book ai didi

java - 找不到符号 setonClickListener

转载 作者:行者123 更新时间:2023-12-01 07:18:04 26 4
gpt4 key购买 nike

我正在尝试在 Android 应用程序中为 recycleview 设置 onlclick 监听器,但尽管一切都正确,但我仍然在 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 接口(interface)代码

    public interface OnItemClickListener {
void onClick(View view, int position);}

我删除了所有导入和包声明行以使代码更短。我可能做错了什么以及如何解决它?谢谢

最佳答案

您已经非常接近解决问题了。你在 Activity 方面做得很好。而且你的思维方式是正确的。

问题在于您将 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 表达式、自定义接口(interface)、事件总线或 react 流。希望对你有帮助:D

关于java - 找不到符号 setonClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51429038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com