我想知道如何在以下行中的每个函数中放入Toast
:
Toast.makeText(OwnerAdapter.this,"Owner sucessfully deleted!",Toast.LENGTH_SHORT).show();
Toast.makeText(OwnerAdapter.this,"Owner updated successfully!",Toast.LENGTH_SHORT).show();
这些函数位于OwnerAdapter
类内部,用于创建一维元素列表。但它不起作用,因为编译器抛出了一个错误:
Cannot resolved method 'makeText(com.example.iury.bookapp.OwnerAdapter,java.lang.String, int)
- 此处编码
public class Owner Adapter extends RecyclerView.Adapter<OnwerAdapter.ViewHolder> {
// Button update
holder.button_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name_owner = holder.editName_owner.getText().toString();
String email_owner = holder.editEmail_owner.getText().toString();
String telephone_owner = holder.editTelephone_owner.getText().toString();
conexao.UpdateOwner(new Owner(f_owner.getId_owner(),name_onwer,email_owner,telephone_onwer));
notifyDataSetChanged();
Toast.makeText(OwnerAdapter.this,"Owner updated successfully!",Toast.LENGTH_SHORT).show();
((Activity) context).finish();
context.startActivity(((Activity) context).getIntent());
}
});
// Button delete
holder.button_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
connection.DeleteOnwer(f_owner.getId_owner());
owner.remove(position);
Toast.makeText(OwnerAdapter.this,"Owner sucessfully deleted!",Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
});
}
您需要将上下文传递给Toast才能使其工作。如果适配器在活动中,则需要显示类似的消息
Toast.makeText(getApplicationContext(),"Owner updated successfully!",Toast.LENGTH_SHORT);
如果适配器在片段中,则需要显示类似的消息
Toast.makeText(getActivity(),"Owner updated successfully!", Toast.LENGTH_SHORT).show();
如果适配器是作为一个单独的java文件编写的,那么可以像一样进行操作
Toast.makeText(v.getContext(),"Owner updated successfully!",Toast.LENGTH_SHORT).show();
或者您可以将上下文传递给适配器,然后将该上下文传递给Toast
Toast.makeText(context,"Owner updated successfully!",Toast.LENGTH_SHORT).show();