S
S
shnicel2016-09-29 14:32:50
Java
shnicel, 2016-09-29 14:32:50

How to properly display items in listview?

ed96de9440174235a61e6300279bf19f.pngright now, everything that is in the line is displayed, but I only need the first value, put it on the true path, how to display only the name of the station?

public class CategoryActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

//        Получаем данные от предыдующей активити
        String Name = getIntent().getStringExtra("Name");
        String Position = getIntent().getStringExtra("Position");

        Map<String, List<String>> genres = new HashMap<>();
        genres.put("0", new ArrayList<String>() {{
            add(String.valueOf(new Station("Radio Record", "http://air2.radiorecord.ru:805/rr_320")));
            add(String.valueOf(new Station("Record Megamix", "http://air2.radiorecord.ru:805/mix_320")));
            add(String.valueOf(new Station("Record EDM", "http://air2.radiorecord.ru:805/mix_320")));
            add(String.valueOf(new Station("Future House", "http://air2.radiorecord.ru:805/fut_320")));
            add(String.valueOf(new Station("161 FM", "http://stream.161.fm:8000/256")));
            add(String.valueOf(new Station("Самые раскрученные хиты", "http://air2.radiorecord.ru:805/mix_320")));
            add(String.valueOf(new Station("BOOMBOOM FM", "http://pub2.player-radio.ru/boomboom/boomboom-128.mp3/icecast.audio")));
            add(String.valueOf(new Station("Danceradio", "http://play.danceradio.ru:9000/8046")));
            add(String.valueOf(new Station("D-FM", "http://icecast.radiodfm.cdnvideo.ru/dfm.mp3")));
            add(String.valueOf(new Station("Euro Dance Radio", "http://188.242.142.150:8010/edr256.mp3")));
            add(String.valueOf(new Station("Europa Plus", "http://stream01.emgsound.ru/europaplus128.mp3")));
            add(String.valueOf(new Station("InSTYLE", "http://46.105.180.202:8038/instyle")));
            add(String.valueOf(new Station("Kiss FM", "http://online-kissfm.tavrmedia.ua/KissFM")));
            add(String.valueOf(new Station("Klubb HQ", "http://radio.promodj.com:8000/klubb-192")));
            add(String.valueOf(new Station("LandFM.com", "http://landfm.com:8000/listen")));
            add(String.valueOf(new Station("NRJ", "http://cast.nrj.in.ua/nrj")));
            add(String.valueOf(new Station("OrClub", "http://89.108.114.143:8000/orclub.mp3")));
            add(String.valueOf(new Station("Aplus.FM", "http://shoutcast.aplus.by:9000/aplus_128")));
            add(String.valueOf(new Station("Radio Bomba", "http://free.radioheart.ru:8000/radiobomba")));
            add(String.valueOf(new Station("SHTORM.FM - CLUB", "http://live.shtorm.fm:8000/mp3_club")));
            add(String.valueOf(new Station("zaycev.fm club", "http://zaycevfm.cdnvideo.ru/ZaycevFM_electronic_256.mp3")));
        }});

        String[] stations = genres.get(Position).toArray(new String[0]);  //Вот это вставляем в arrayadapter

//        Устанавливаем имя выбранной категории
        setTitle(Name);

        ListView Stations = (ListView) findViewById(R.id.Stations);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stations);
        Stations.setAdapter(adapter);

    }

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2016-09-29
@shnicel

We need to add the toString method to the Station class to correctly convert it to a string:

class Station {
    private String name;
    private URL url;
 
    public Station(String name, URL url) {
        this.name = name;
        this.url = url;
    }
 
    @Override
    public String toString() {
        return this.name;
    }
 
    // Здесь должен быть набор гетеров и сеттеров
}
 
Map<String, List<Station>> genres = new HashMap<>();
genres.put("Dance", new ArrayList<String>() {{
    add(new Station("Radio Record", "http://air2.radiorecord.ru:805/rr_320"));
    // ...
}});

Station[] stations = genres.get(genreName).toArray(new Station[0]);

ArrayAdapter<Station> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, stations);

D
davidnum95, 2016-09-29
@davidnum95

Something just took you in the wrong direction. For lists, use RecyclerView.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question