0

I'm trying to customize a Dialog. It should take Strings from the DB and populate a list. My Dialog:

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

}

My Adapter (in order to customize each row):

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

}

}

The application doesn't throw any exception. The Dialog shows up...but empty. I see only the title and no rows entry. I made some tries, using Log.e in order to see if the code is run or not. The getView of the adapter is never called, the onCreate of the Dialog is. I think that the problem is in the two following rows of my Dialog, but i don't know how to fix it:

    listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f));
    listView.setId(android.R.id.list);
Emaborsa
  • 2,360
  • 4
  • 28
  • 50
  • You're probably right. What layout params are you using, because it looks like you're using the ones for a linear layout, while the view you're dealing with is a listview. – skUDA Jul 13 '12 at 20:03
  • ok, i understand, but i don't know how to solve it. Could you help me a little bit more? – Emaborsa Jul 13 '12 at 20:08
  • 1
    take a look at the [listview api](http://developer.android.com/reference/android/widget/ListView.html). You need to use ViewGroup.LayoutParams, which only has two parameters in its constructor vs LinearLayout.LayoutParams which has a constructor with three parameters. But, as has been pointed out below, it's not the root of your problem. – skUDA Jul 13 '12 at 20:11
  • ^^ I arranged it. Thanks for the hint! – Emaborsa Jul 13 '12 at 20:24
  • ...anyway, you where right. It was only one of many problems. Doing in this way i'm not able to "style" the dialog anymore. The items aren't clickable anymore. I think i'll look for another solution. – Emaborsa Jul 13 '12 at 21:58
  • there is another way to make them clickable. Instead of an OnItemClickListener on the listview, in your adapter's getView do rowView.setOnClickListener(new View.OnItemClickListener(){ public void onClick(){//your code here}}); – skUDA Jul 13 '12 at 22:02
  • i've seen...but there are other things i don't like. For example the black shadow at top and bottom, are not well aligned, so i have a gap of another color. I could remove the shadow, but than the user doesn't see that there is a scroll. I could also fill this gap of the same color, but than there is even a horrible white border around the listview. Furthermore i would like to have the dialog with rounded corner....i tryied to do it using styles, but it doesn't work. the last thing is that the activity behind is completely hidden. – Emaborsa Jul 13 '12 at 22:54

2 Answers2

2

looks like your adapter does not know anything about how many entries it has. try to override the getCount() function in your custom listadapter. or use the superclass constructor super(context, R.layout.countries_row, countries);

SimonSays
  • 10,867
  • 7
  • 44
  • 59
0

After trying and trying I arranged it and i wanted to share knowledge. I removed the following lines from my Dialog:

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

and in the adapter i changed the super constructor, changing the passed layout and the items:

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

In the adapter I also had to add a listener to each row and handle the events....

Emaborsa
  • 2,360
  • 4
  • 28
  • 50