简体   繁体   中英

Android: Creating custom Dialog with List

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

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

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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM