我正在尝试自定义一个对话框。它应该从数据库中获取字符串并填充一个列表。我的对话框:
public class CountriesDialog extends AlertDialog {
private Context context;
private Vector<String> countries;
private ListView listView;
private CountriesAdapter adapter;
public CountriesDialog(Context context, Vector<String> countries) {
super(context);
this.context = context;
this.countries = countries;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
listView = new ListView(context);
listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f));
listView.setId(android.R.id.list);
Log.e("WORKS", " "+(listView == null) + " asas");
adapter = new CountriesAdapter(context, countries);
listView.setAdapter(adapter);
listView.setOnItemClickListener(adapter);
setInverseBackgroundForced(true);
setView(listView);
setTitle("Select country");
super.onCreate(savedInstanceState);
}
}
我的适配器(为了自定义每一行):
public class CountriesAdapter extends ArrayAdapter<String> implements OnItemClickListener {
private Vector<String> countries;
private Context context;
public CountriesAdapter(Context context, Vector<String> countries) {
super(context, R.layout.countries_row);
this.context = context;
this.countries = countries;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.countries_row, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.countries_list_single_image);
TextView textView = (TextView) rowView.findViewById(R.id.countries_list_single_text);
textView.setText(countries.get(position));
DatabaseManager db = new DatabaseManager(context);
db.connectForReading();
String recource = db.getFLagName(countries.get(position));
db.disconnect();
int resId = context.getResources().getIdentifier(recource, "drawable", "com.emaborsa");
imageView.setImageDrawable(context.getResources().getDrawable(resId));
Log.e("WORKS", recource );
return rowView;
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
该应用程序不会引发任何异常。对话框出现了……但为空。我只看到标题,没有行条目。我做了一些尝试,使用 Log.e 来查看代码是否运行。适配器的 getView 永远不会被调用,Dialog 的 onCreate 是。我认为问题出在对话框的以下两行中,但我不知道如何解决:
listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f));
listView.setId(android.R.id.list);