转换的ArrayList<类>字符串[]字符串、ArrayList、LT、GT

2023-09-04 05:08:55 作者:戒烟戒酒戒相思

我有类Person

class person{
int ID ;
sting FirstName ;
string Last Name ;
String Telephone ;

}

和我有的ArrayList<人> ;

现在我要显示一个仅包含姓+,+名字的简单列表视图

now I want to display simple list view containing just FirstName + "," + "LastName"

这样我就可以用code像

so I can use code like

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");

ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);

builder.setView(modeList);
final Dialog dialog = builder.create();

dialog.show();

所以如何cconvert ArrayList的字符串[]包含格式姓+,+姓氏

我可以循环,但有没有什么好的办法或简单的方法来做到这一点,因为我试图使用适配器,但它没有出现在dialoge

I can loop , but Is there any good way or simple way to do that , because I tried to use adapter but it failed to appear in the dialoge

推荐答案

请您 ArrayAdapter 使用类型而不是字符串(也只是通过了的ArrayList&LT;人&GT; 而不是数组)是这样的:

Make your ArrayAdapter to use the type person and not String (also, just pass the ArrayList<person> instead of an array) like this:

ArrayAdapter<person> modeAdapter = new ArrayAdapter<person>(this, android.R.layout.simple_list_item_1, android.R.id.text1, theArrayList);

然后重写的课的toString 方法:

class person {
        int ID;
        String FirstName;
        String LastName;
        String Telephone;

        @Override
        public String toString() {          
            return "Whatever " + FirstName + " and Whatever  " + LastName;
        }

    }