0

我正在尝试自定义一个对话框。它应该从数据库中获取字符串并填充一个列表。我的对话框:

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);
4

2 回答 2

2

看起来您的适配器不知道它有多少条目。尝试覆盖自定义列表适配器中的 getCount() 函数。或使用超类构造函数 super(context, R.layout.countries_row, countries);

于 2012-07-13T20:01:29.870 回答
0

在尝试和尝试之后,我安排了它,我想分享知识。我从对话框中删除了以下几行:

 listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f));
 listView.setOnItemClickListener(adapter);
 setInverseBackgroundForced(true);

在适配器中,我更改了超级构造函数,更改了传递的布局和项目:

super(context, R.layout.countries, countries);

在适配器中,我还必须为每一行添加一个侦听器并处理事件......

于 2012-07-14T08:09:32.133 回答