简体   繁体   中英

listview in dialougbox shows nothing

i have this code to show listview in dialogbox frame of listview is shown but without any text in it(i have background colored listview )

 public void shw(View view)
    {

        Dialog dialog=new Dialog(MainActivity.this);
        dialog.setTitle("Dialog Title");
        dialog.setContentView(R.layout.custadd);
        LayoutInflater inflater = getLayoutInflater();
        View convertView = (View) inflater.inflate(R.layout.custadd,null);
        ListView lv=(ListView) convertView.findViewById(R.id.listv);
        String s1[]={"sunday","monday","tuesday"};
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this,
                R.layout.simple_list_item, s1 );
        lv.setAdapter(arrayAdapter);
        dialog.show();

    }

Try this

public void shw(View view) {

    Dialog dialog=new Dialog(MainActivity.this);
    dialog.setTitle("Dialog Title");     
    dialog.setContentView(R.layout.custadd);    
    ListView lv=(ListView) dialog.findViewById(R.id.listv);    
    String s1[]={"sunday","monday","tuesday"};

     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1, s1 );
    lv.setAdapter(arrayAdapter);
    dialog.show();

}

Yes, because the view you display ListView is not the view you set for your Dialog . You should fix as below:

Dialog dialog=new Dialog(MainActivity.this);
dialog.setTitle("Dialog Title");
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.custadd,null    );
dialog.setContentView(convertView); // set convertView as content view. 
// you can find listview and then fill data here

Remove dialog.setContentView(R.layout.custadd); and set view for dialog when it get's inflated dialog.setContentView(convertView); and the issue will be resolved.

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