美文网首页
读取main/assets目录下的text文本

读取main/assets目录下的text文本

作者: alialiali | 来源:发表于2017-05-02 17:37 被阅读0次

    text文本的json字符串,计算counter_fee的总和
    {
    "price_floor":0,
    "price_ceil":1000000,
    "data":[
    {
    "accident_floor":1,
    "accident_ceil":2,
    "loss_acquire":true,
    "new_car":false,
    "CLIVTA":{
    "counter_fee":"0",
    "risk_grade_1":"C01",
    "risk_grade_2":"C23"
    },
    "VI":{
    "counter_fee":"26",
    "risk_grade_1":"C01",
    "risk_grade_2":"C15"
    }
    },
    {
    "accident_floor":0,
    "accident_ceil":0,
    "loss_acquire":false,
    "new_car":true,
    "CLIVTA":{
    "counter_fee":"2",
    "risk_grade_1":"C01",
    "risk_grade_2":"C23"
    },
    "VI":{
    "counter_fee":"26",
    "risk_grade_1":"C01",
    "risk_grade_2":"C15"
    }
    },
    {
    "accident_floor":1,
    "accident_ceil":2,
    "loss_acquire":false,
    "new_car":false,
    "CLIVTA":{
    "counter_fee":"10",
    "risk_grade_1":"C01",
    "risk_grade_2":"C23"
    },
    "VI":{
    "counter_fee":"16",
    "risk_grade_1":"C01",
    "risk_grade_2":"C15"
    }
    }
    ]
    }
    配置文件:
    compile 'com.google.code.gson:gson:2.3.1'
    参考所有代码:
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    import com.google.gson.Gson;
    import java.io.InputStream;
    import java.util.List;

    public class MainActivity extends AppCompatActivity {
    TextView tv;
    List<TestBean.DataBean> data;
    private Object tatolCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv_text);
    
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoSecondActivity();
            }
        });
        getTextData();
    
    }
    private void gotoSecondActivity() {
        startActivity(new Intent(this,SecondActivity.class));
    }
    private void getTextData(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    //获得AssetManger 对象, 调用其open 方法取得  
                    //对应的inputStream对象
                    InputStream is = getAssets().open("test.txt");
                    int size = is.available();//取得数据流的数据大小
                    byte[] buffer = new byte[size];
                    is.read(buffer);
                    is.close();
                    String txt = new String(buffer);
                    tv.setText(txt);
    
                    TestBean testBean = new Gson().fromJson(txt, TestBean.class);
                    data = testBean.getData();
    
    
                } catch (Exception e) {
                    Log.e("Assert", e.toString());
                }
            }
        });
        getTatolCount(data);
    }
    
    public void getTatolCount(List<TestBean.DataBean> data) {
        int count = 0;
        for (int i= 0;i< data.size();i++){
            String counter_fee = data.get(i).getCLIVTA().getCounter_fee();
            int i1 = Integer.parseInt(counter_fee);
            String counter_fee1 = data.get(i).getVI().getCounter_fee();
            int i2 = Integer.parseInt(counter_fee1);
            count += i1+i2;
        }
        Log.e("TAG","count"+count);
    }
    

    }

    相关文章

      网友评论

          本文标题:读取main/assets目录下的text文本

          本文链接:https://www.haomeiwen.com/subject/ejfhtxtx.html