Assets+Json+ScrollView练习

案例1:

需要创建Assets文件夹 并在该文件夹下创建json.txt文件  放入json字符串

导入依赖

compile 'com.google.code.gson:gson:2.8.2'

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.mrzhao.exam1demo.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="解析"
        tools:ignore="OnClick" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/show_tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:textSize="20sp" />
    </ScrollView>

</LinearLayout>

代码:

public class MainActivity extends AppCompatActivity {

    private TextView showTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showTv = (TextView) findViewById(R.id.show_tv);
    }

    public void onClick(View view) {
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream  = null;
        try {
            //读取数据
            inputStream = getAssets().open("json.txt");
            //实例化
            outputStream = new ByteArrayOutputStream();
            int len = 0;
            byte []  bytes = new byte[1024];
            //循环读取
            while ((len =  inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
            }
            String result = outputStream.toString();
            //设置展示大文本上
            showTv.setText(result);

        //  解析
            Gson gson = new Gson();
            FoodEntity foodEntity = gson.fromJson(result, FoodEntity.class);

            //添加到  文本上
            showTv.append("\n\n\n\n\n"+foodEntity.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

案例二:

需要创建Assets文件夹 并在该文件夹下创建json.txt文件  放入json字符串

导入依赖

compile 'com.google.code.gson:gson:2.8.2'

在res/drawable文件夹下创建资源文件 :bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <stroke
        android:width="2dp"
        android:color="@color/colorAccent"></stroke>
</shape>

MainActivity布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.mrzhao.exam2demo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="用户名:"
            android:textSize="20sp" />

        <EditText
            android:padding="5dp"
            android:id="@+id/userName_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/bg"
            android:hint="请输入用户名" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"

            android:text="密    码:"
            android:textSize="20sp" />

        <EditText
            android:padding="5dp"
            android:id="@+id/userPassword_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/bg"
            android:hint="请输入密码" />
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="20dp"
        android:onClick="onClick"
        android:text="登    陆"
        tools:ignore="OnClick" />
</LinearLayout>

SecondActivity布局文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mrzhao.exam2demo.SecondActivity">

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"></LinearLayout>
</ScrollView>

MainActivity:

public class MainActivity extends AppCompatActivity {

    private EditText userNameEt;
    private EditText userPasswordEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userNameEt = (EditText) findViewById(R.id.userName_et);
        userPasswordEt = (EditText) findViewById(R.id.userPassword_et);
    }

    public void onClick(View view) {
        //点击登陆

        String userName = userNameEt.getText().toString();
        String userPassword = userPasswordEt.getText().toString();
        if (TextUtils.isEmpty(userName)) {
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(userPassword)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        //读取资源
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        try {
            inputStream = getAssets().open("json.txt");
            outputStream = new ByteArrayOutputStream();
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len =  inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes,0,len);
            }
            //读取出来的结果
            String result = outputStream.toString();

            //解析
            Gson gson =  new Gson();
            NewsBean newsBean = gson.fromJson(result, NewsBean.class);

            //跳转到B页面

            Intent intent = new Intent(this,SecondActivity.class);
            //传递解析的对象
            intent.putExtra("food",newsBean);
            startActivity(intent);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

SecondActivity:

public class SecondActivity extends AppCompatActivity {

    private LinearLayout container;
    private List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        //获取 容器视图
        container = (LinearLayout) findViewById(R.id.container);
        //获取传递过来的数据
        Intent intent = getIntent();
        NewsBean news = (NewsBean) intent.getSerializableExtra("food");
        //取出集合
        List<NewsBean.ParamzBean.FeedsBean> feeds = news.getParamz().getFeeds();
        // 循环取出Subject列
        for (int i = 0; i < feeds.size(); i++) {
            String subject = feeds.get(i).getData().getSubject();
            list.add(subject);
        }

        // 循环创建视图
        for (int i = 0; i < list.size(); i++) {
            //创建一个TextView
            TextView view = new TextView(this);
            view.setTextSize(30);
            //设置文本展示的内容
            view.setText(list.get(i));

            // 添加到容器 LinearLayout中
            container.addView(view);


        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值