Search code examples
javaandroidintellij-ideastaticadapter

I can not configure a personal adapter


Good day to all! Please help with the problem. I want the ALERTDIALOG to open when I click on the button. This dialog is implemented in the DialogFactors class, and is called in MainActivity:

MainActivity

AlertDialog dialog = DialogFactory.getDialog(MainActivity.this);

In the DialogFactory, I create my (class) personal adapter, configure it. I want to create a variable for this adapter, but I write "this" to the CONTEXT variable and it throws an error.

public class DialogFactory extends ListActivity {

    public static AlertDialog getDialog(Activity activity) {

        View view = activity.getLayoutInflater().inflate(R.layout.lsview, null); // 2. Создал вьюшку
        ListView lv = (ListView) view.findViewById(R.id.lisV); // 1. Создаю ListView
        String values[] = { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" }; // 3. Массив с данными


        AlertDialog.Builder builder = new AlertDialog.Builder(activity);


        // use your custom layout

        MyAdapter adapter = new MyAdapter(activity, values ); //error

        lv.setAdapter(adapter);
        builder.setView(view);
        return builder.create();
    }

    class MyAdapter extends ArrayAdapter<String>{
        Context context;
        String rvalues[];

        public MyAdapter(Context c, String values[]) {
            super(c, R.layout.listviex, R.id.textView12);
            this.context = c;
            this.rvalues = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater li = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View lx = li.inflate(R.layout.listviex, parent, false); // 2. Создал вьюшку
            TextView tv = lx.findViewById(R.id.textView12);
            tv.setText(rvalues[position]);
            return lx;
        }
    }
}

Solution

  • you must make your getDialog not static or make your MyAdapter static !

    public AlertDialog getDialog(Activity activity) {...
    

    or

     public static class MyAdapter extends ArrayAdapter<String> {...
    

    UPDATE :

    if you make your getDialogto not static then you must get new instance of your DialogFactoryclass first :

    DialogFactory dialogFactory = new DialogFactory();
    

    then :

    AlertDialog dialog = dialogFactory.getDialog(this);