Android:使用List创建自定义对话框

时间:2012-07-13 19:46:18

标签: android customdialog

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

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,对话框的onCreate是。 我认为问题出现在我的Dialog的两行中,但我不知道如何修复它:

    listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f));
    listView.setId(android.R.id.list);

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

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

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

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

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