祝大家有美好的一天!请帮助解决问题。我希望在单击按钮时打开ALERTDIALOG。此对话框在DialogFactors类中实现,并在MainActivity中调用:
主要活动
AlertDialog对话框= DialogFactory.getDialog(MainActivity.this);
在DialogFactory中,创建我的(类)个人适配器,对其进行配置。我想为此适配器创建一个变量,但是我将“ this”写入CONTEXT变量,并引发错误。
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;
}
}
}
最佳答案
您必须将您的getDialog
设为非静态或将MyAdapter
设为静态!
public AlertDialog getDialog(Activity activity) {...
要么
public static class MyAdapter extends ArrayAdapter<String> {...
更新:
如果将
getDialog
设置为非静态,则必须首先获取DialogFactory
类的新实例:DialogFactory dialogFactory = new DialogFactory();
然后 :
AlertDialog dialog = dialogFactory.getDialog(this);