0

I figure it's easy enough to copy a text file or CSV file from my computer to the phone, but I need to the app to read in the text file and be able to randomize a list from clicking the button.

I have created a very simple app that takes a list of movie titles and randomizes that list into a listview when clicking the button.

Now I need to download a text or CSV file, that contains a larger list into the app.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button getList;
ListView movieList;
ArrayList<String> moviePicks;
long seed = System.nanoTime();



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getList = (Button) findViewById(R.id.btnGetList);
    getList.setOnClickListener(this);

    movieList = (ListView) findViewById(R.id.LV_Movies);

    moviePicks = new ArrayList<String>();

    moviePicks.add("Star Wars: A New Hope");
    moviePicks.add("Star Wars: Revenge of the Jedi");
    moviePicks.add("Die Hard");
    moviePicks.add("The Pink Panther");
    moviePicks.add("Hunt for Red October");
    moviePicks.add("Swat");
    moviePicks.add("Entrapment");
    moviePicks.add("Back to the Future");
    moviePicks.add("Jaws");
    moviePicks.add("The Fast and the Furious");
    moviePicks.add("Rounders");
    moviePicks.add("Pearl Harbor");
    moviePicks.add("Doom");
    moviePicks.add("Hells Gate");
    moviePicks.add("Avatar");
    moviePicks.add("Coaster");
    moviePicks.add("Brick Mansions");


}

@Override
public void onClick(View view) {

    Collections.shuffle(moviePicks, new Random(seed));

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, moviePicks);

    movieList.setAdapter(arrayAdapter);

}

How can I have a text file read in as the list to be randomized?

grooveplex
  • 2,492
  • 4
  • 28
  • 30

1 Answers1

0

If you want to have some default data built into the app, I'd suggest adding the CSV into your assets folder and then reading with a FileInputStream--check out this SO post for more info:

Reading CSV file in resources folder android

Sagar Poshiya
  • 136
  • 3
  • 11
johnheroy
  • 416
  • 2
  • 7
  • It's not necessarily "default" data. The actual list will be updated from time to time, based on the addition or subtraction of items in the list. So being able to upload the list when the list itself has been changed is a primary task. – Dave Henderson Feb 07 '19 at 17:46
  • @DaveHenderson have you considered then hosting the CSV file on a remote server? I think that would be better UX than asking users to open a CSV locally on their device (it's possible but Android doesn't handle local files very well) – johnheroy Feb 07 '19 at 22:07
  • heh...yeah, I'm finding that out rather quickly, and there in-lies the challenge. Right now, the list it's using is a movie list. But what if I wanted to change that list to a list of cars, or vegetables? The ability to change out the list is really the key. I could grab a file from DropBox to upload to the app, but storing it on the device and then reading from it is still the hang up. – Dave Henderson Feb 08 '19 at 14:29