如何从适配器视图中的嵌套onclicklistener中访问活动实例?我尝试了各种各样的事情(context / getcontext()),但它们不起作用。我在检查许可中遇到了以下声明中的问题。
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.delegate_access_listview_item, parent, false);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
final Dialog dialog = new Dialog(context);
dialog.setTitle("Delegate");
dialog.setContentView(R.layout.dialog_deleagte_access_profile);
final Button callBtn = (Button) dialog.findViewById(R.id.call_btn);
//callBtn.setText();
Button emailBtn = (Button) dialog.findViewById(R.id.call_btn);
//emailBtn.setText();
CircularImageView imgView = dialog.findViewById(R.id.imageView2);
// TODO: set image drawable
dialog.show();
callBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
int checkPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
if (checkPermission != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(
context,
new String[]{Manifest.permission.CALL_PHONE},
REQUEST_CALL);
}
else
{
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("373548474"));
context.startActivity(callIntent);
}
}
});
}
});
return new MyViewHolder(v);
}
答案 0 :(得分:1)
这不是你应该如何实现的。请尝试以下步骤:
1)在onCreateViewHolder上设置点击监听器,就像你一样。
2)当触发onClick事件时,不是在此处实现对话,而是向活动发送回调。
3)在活动中调用回调时显示对话框。
通过这种方式,您将拥有活动访问权限和干净的代码
答案 1 :(得分:0)
请创建根源文件夹的Application Class并添加AndroidManifest.xml文件。
ApplicationClass.java
public class ApplicationClass extends Application {
private static ApplicationClass getInstance;
@Override
public void onCreate() {
super.onCreate();
getInstance = this;
}
public static synchronized ApplicationClass getInstance() {
return getInstance;
}
public static Context getContext() {
return getInstance;
}
}
的AndroidManifest.xml
<application
android:name=".ApplicationClass"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"></application>
AdapterClass.java
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.delegate_access_listview_item, parent, false);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
final Dialog dialog = new Dialog(ApplicationClass.getInstance());
dialog.setTitle("Delegate");
dialog.setContentView(R.layout.dialog_deleagte_access_profile);
final Button callBtn = (Button) dialog.findViewById(R.id.call_btn);
//callBtn.setText();
Button emailBtn = (Button) dialog.findViewById(R.id.call_btn);
//emailBtn.setText();
CircularImageView imgView = dialog.findViewById(R.id.imageView2);
// TODO: set image drawable
dialog.show();
callBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
int checkPermission = ContextCompat.checkSelfPermission(ApplicationClass.getInstance(), Manifest.permission.CALL_PHONE);
if (checkPermission != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(
context,
new String[]{Manifest.permission.CALL_PHONE},
REQUEST_CALL);
}
else
{
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("373548474"));
context.startActivity(callIntent);
}
}
});
}
});
return new MyViewHolder(v);
}