如何导入文本列表或CSV文件?

时间:2019-02-07 17:14:27

标签: android listview arraylist import

我认为将文本文件或CSV文件从计算机复制到手机很容易,但是我需要使用该应用程序来读取文本文件,并能够通过单击按钮将列表随机化。

我创建了一个非常简单的应用,该应用可以获取电影标题列表,并在单击按钮时将该列表随机化为列表视图。

现在,我需要将包含较大列表的文本或CSV文件下载到应用中。

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

}

我如何读取文本文件作为要随机分配的列表?

1 个答案:

答案 0 :(得分:0)

如果您想在应用程序中内置一些默认数据,建议您将CSV添加到资产文件夹中,然后使用FileInputStream进行阅读-请查看此SO帖子以获取更多信息:

Reading CSV file in resources folder android