S
S
shnicel2016-09-28 20:12:06
Java
shnicel, 2016-09-28 20:12:06

How to do array substitution in ArrayAdapter?

Guys, I’m completely new, I can’t understand one thing, I call a new activity, I transfer data to it

Category.putExtra("Name", catName);
Category.putExtra("Position", catPosition);

In the opened activity
String Name = getIntent().getStringExtra("Name");
String Position = getIntent().getStringExtra("Position");
an array should be selected that matches the Name value, but I don’t understand how to do this
public class CategoryActivity extends AppCompatActivity {

    String[] Dubstep = { "Record Dubstep" };
    String[] Dance = { "Radio Record", "Record Megamix", "Record EDM", "Future House" };
    String[] Trance = { "Trancemission", "109 FM UKRAINE", "TranceONE" };

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

//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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

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

        ListView Stations = (ListView) findViewById(R.id.Stations);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Вот здесь?);
        Stations.setAdapter(adapter);

    }

}

Answer the question

In order to leave comments, you need to log in

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

Combine three separate arrays into one HashMap and select the desired list from it.

Map<String, List<String>> genres = new HashMap<>();
genres.put("Dubstep", new ArrayList<String>() {{
    add("Record Dubstep");
}});
genres.put("Dance", new ArrayList<String>() {{
    add("Radio Record");
    add("Record Megamix");
    add("Record EDM");
    add("Future House");
}});
genres.put("Trance", new ArrayList<String>() {{
    add("Trancemission");
    add("109 FM UKRAINE");
    add("TranceONE");
}});

String[] stations = genres.get(Name).toArray(new String[0]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question