开发者

ClassCastException when setting onClickListener within Dialog subclass

开发者 https://www.devze.com 2023-03-16 15:53 出处:网络
I have subclassed Dialog in order to display a popup. This dialog contains a ListView, and so I also have an inner class (in the Dialog) that subclasses BaseAdapter.

I have subclassed Dialog in order to display a popup. This dialog contains a ListView, and so I also have an inner class (in the Dialog) that subclasses BaseAdapter.

I am trying to set the onClickListener for text that is within my list, however I keep getting ClassCastException at setOnClickListener (see code below).

 public class CustomDialog extends Dialog
 {
MyAdapter adapter = null;

public CustomDialog(Context context) 
{
    super(context);
    setContentView(R.layout.custom_popup);

    ListView listView = (ListView) findViewById(android.R.id.list);
    adapter = new MyAdapter(context);
    listView.setAdapter(adapter);
}


     public class MyAdapter extends BaseAdapter implem开发者_如何学运维ents OnClickListener
     {
       @Override
       public View getView(int arg0, View arg1, ViewGroup arg2) 
        {
                ....
            TextView groupText = (TextView)v.findViewById(R.id.mytext);
            mytext.setOnClickListener((android.view.View.OnClickListener) this); //crashes here
                ....
         }

        @Override
        public void onClick(DialogInterface arg0, int arg1) 
        {
        }
     }
}


In this case, you're messing with two classes that have the same name but different package....View.OnClickListener and DialogInterface.OnClickListener. The listener you have in your class is a DialogInterface.OnClickListener but you're wanting a View.OnClickListener. Change your implement to use View.OnClickListener and that'll fix your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消