自定义对话框包含RecyclerView时遇到的context问题

我想做一个排行榜,就是用自定义的对话框包含一个recyclerView实现展示。以下是自定义的对话框类部分。在写到设置recyclerView的layoutManager的时候遇到问题,context不知道怎么处理,也就是“LinearLayoutManager layoutManager=new LinearLayoutManager(**this**);”的this报错,说:
LinearLayoutManager(android.content.Context)in LinearLayoutManager cannot be applied to (com.example.schoolpet.MyDialog)
由于我对android的context实在还没能理解,求助各位

public class MyDialog extends Dialog {
Activity context;
public RecyclerView recyclerView;
private View.OnClickListener mClickListener;
private List rankingLists=new ArrayList<>();
public Context context1;
public PetPage petPage;

public MyDialog(Activity context) {
    super(context);
    this.context = context;
}

public MyDialog(Activity context, int theme, View.OnClickListener clickListener) {
    super(context, theme);
    this.context = context;
    this.mClickListener = clickListener;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 指定布局
    this.setContentView(R.layout.ranking_list);

    initRankingList();
    recyclerView=(RecyclerView)findViewById(R.id.rankingList_recyclerView);
    LinearLayoutManager layoutManager=new LinearLayoutManager(**this**);
    recyclerView.setLayoutManager(layoutManager);
    RankingListAdapter adapter=new RankingListAdapter(rankingLists);
    recyclerView.setAdapter(adapter);

    /*
     * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
     * 对象,这样这可以以同样的方式改变这个Activity的属性.
     */
    Window dialogWindow = this.getWindow();
    WindowManager m = context.getWindowManager();
    Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
    WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
    p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
    p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.8
    dialogWindow.setAttributes(p);

    this.setCancelable(true);

}

private void initRankingList() {
    RankingList rankingList=new RankingList(1,"林辉",30,30);
    rankingLists.add(rankingList);
    RankingList rankingList2=new RankingList(2,"林辉",30,30);
    rankingLists.add(rankingList);
    RankingList rankingList3=new RankingList(3,"林辉",30,30);
    rankingLists.add(rankingList);
    RankingList rankingList4=new RankingList(4,"林辉",30,30);
    rankingLists.add(rankingList);
}

}

2个回答

把 ** ** 都去了 就差不多了。

(**this**)换成context

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问