从资产android

时间:2015-05-31 16:16:04

标签: android io

我正在尝试在Android中为卡片游戏构建一个卡片查看器。我的计划是使用arraylist将卡填充到数据库中。我会读取文件中的所有值,并通过listview显示。我的listview代码和arraylist代码很好(在不使用文件输入的情况下进行测试)。当我尝试从文件读入时发生此问题。我不知道该怎么办。当我像这样运行程序时,它会崩溃。当我删除“parseInt”方法(因为我正在读取文件中的整数)时,它会运行,但我的数据都没有被填充。我的io代码出了什么问题?

    import android.content.Intent;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;

    import java.util.ArrayList;
    import java.io.*;

    import android.widget.ArrayAdapter;
    import android.widget.ListView;


    public class DeckBuilderActivity extends ActionBarActivity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
     Intent intent = getIntent();
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_deck_builder);

    //populate       database************************************************************************
    ArrayList<Card> database = new ArrayList<Card>(); //create new arraylist of datatype card

    //value holders
    String cardName, expansion, cardNum, unitType, unitClan, unitRace, trigger, unitSkill, cardLore,
            imageId;
    int unitGrade, unitPower, unitShield;
    //exception handling

    try{
        String myfile = "cardmaindatabase.txt";
        InputStream inputReader = getAssets().open(myfile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputReader)); //open file
        //begin populating with a for loop
        int i = 0; //incrementer
        while(reader.readLine() != null) //while not end of file
        {
            //assign values to placeholders

            //add new card and take parameters
            database.add(new Card());
            database.get(i).setCardName(reader.readLine());
            database.get(i).setExpansion(reader.readLine());
            database.get(i).setCardNum(reader.readLine());
            database.get(i).setUnitGrade(Integer.parseInt(reader.readLine()));





        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    //******************************************************************************************
    //populate arraylist
    ArrayAdapter<Card> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, database);
    ListView lv = (ListView) findViewById(R.id.card_database_listview);
    lv.setAdapter(adapter);


}

1 个答案:

答案 0 :(得分:1)

        StringBuffer stringBuffer = new StringBuffer();

        try {

            InputStream is = mContext.getResources().getAssets().open("your-file-path");
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line;

            while ((line = br.readLine()) != null) {
                //play with each line here 
            }
}...

尝试使用此代码从资产中获取数据。

或者,如果您不想清除您的代码。尝试像这样修改你的代码。我认为问题出在你的循环中。

String line = null;

while((line = reader.readLine()) != null) //while not end of file
        {
            //play with each line here

        }

但收集/存储此类数据不稳定且难以维护。我建议使用JSON。将数据存储在资产中,以便您能够尽快收集和存储数据。