0

I want to populate a listview from an ArrayList received from another activity, but I don't know how to show only the name of the country.

This is what I have tried. This will only show the address of my countries, not the name.

lv = (ListView) findViewById(R.id.listViewCountries);

lv.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, countries));

I expect to have a listView full of only the names of the countries and when I press on one of them to have an activity open to show info about that country.

Shankar
  • 2,890
  • 3
  • 25
  • 40

2 Answers2

0

Because you are using default implementatino, such as FragmentList, you have prebuilt xml for item list and hence, cannot change it in right way. You need to implement you custom implementation, with new RecyclerView.

Please search for the information, before asking this question. Basic sample you can find in another answer.

GensaGames
  • 5,538
  • 4
  • 24
  • 53
0

You could create another array, like :

Arraylist <String> array2 = new ArrayList() ;
for (Country country : countries){
 array2.add(country.getname())}

And then in your spinner you put array2 instead of countries.

Since you added them in the same order, their index will be the same than in your countries array, then if you want to get à country back, in your onselected item listener, for example, you just have to out sthg like

Country selectedcountry = countries.get(position);

This is how I did it for a list of contacts :

String [] contractList = new String[user.getUserContractsList().size()];
        for (int i = 0; i < user.getUserContractsList().size(); i ++){
            contractList[i] = user.getUserContractsList().get(i).getCONTRACT_NAME();
        }
        //Setting the spinner
        ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, contractList);
        spin.setAdapter(adapter);
        spin.setOnItemSelectedListener(this);

And then to get back the contact value when the user selects an item :

@Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        contract = user.getUserContractsList().get(position);
    }

I don't know if this is clear to you ?

Lolfish
  • 66
  • 6