0

I have class Person

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

}

and I have ArrayList<person> ;

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

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

so how to cconvert the ArrayList to string[] that contains the format FirstName + "," + "LastName"

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

AMH
  • 6,363
  • 27
  • 84
  • 135

2 Answers2

2

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

and then override the person's class toString method:

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

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

    }
user
  • 86,916
  • 18
  • 197
  • 190
  • Wooow , it like invention , u saved me really , so fast too – AMH Jul 01 '12 at 10:46
  • now I have listview I display on dialoge after click button , Iwant when click on item of list , fill anothe control with the textview , it always crash any idea – AMH Jul 01 '12 at 11:18
  • @AMH You should ask another question and provide the full code that you use plus the log with the exception you get. – user Jul 01 '12 at 11:40
  • please check http://stackoverflow.com/questions/11281978/fill-control-after-click-list – AMH Jul 01 '12 at 12:37
0

Better to use Custom Adapter. Here you do not need to convert ArrayList to String ArrayList. You able to set data via array-list position.

link

Thanks

Community
  • 1
  • 1
Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37