第23天~又是JSON+ListView

又是JSON

开新专案
https://ithelp.ithome.com.tw/upload/images/20220204/20119035FZxsenvBNG.png

准备XML档+ListView
https://ithelp.ithome.com.tw/upload/images/20220204/20119035zo1o4jk4Ym.png

放好ID


准备一个TXT档案-

https://ithelp.ithome.com.tw/upload/images/20220204/20119035c2xXc7LHgQ.png


把txt档案贴到java档里

先宣告变数-

https://ithelp.ithome.com.tw/upload/images/20220204/20119035ZD9lYmJ0bK.png

TXT档案贴上会自动解析成字串格式-

{
    title:JSONParserTutorial,
    array:[
    {
       company:Google
    },
    {
       company:Facebook
    },
    {
       company:LinkedIn
    },
    {
       company:Microsoft
    },
    {
       company:Apple
    },
    ],

       nested:{
       flag:ture,
       random_number:1

    }
}

https://ithelp.ithome.com.tw/upload/images/20220204/20119035mpbZUTHT03.png

绿色都是要得~

宣告按钮跟容器-

https://ithelp.ithome.com.tw/upload/images/20220204/20119035Z5NrpRrwAW.png

package com.huang.myjson2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //宣告变数
    String json_string = "{\n" +
            "    title:JSONParserTutorial,\n" +
            "    array:[\n" +
            "    {\n" +
            "       company:Google\n" +
            "    },\n" +
            "    {\n" +
            "       company:Facebook\n" +
            "    },\n" +
            "    {\n" +
            "       company:LinkedIn\n" +
            "    },\n" +
            "    {\n" +
            "       company:Microsoft\n" +
            "    },\n" +
            "    {\n" +
            "       company:Apple\n" +
            "    },\n" +
            "    ],\n" +
            "\n" +
            "       nested:{\n" +
            "       flag:ture,\n" +
            "       random_number:1\n" +
            "\n" +
            "    }\n" +
            "}";

    ListView listView;
    ArrayList<String> items;

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

        //初始化
        
    }
}

再来是初始化-

反红是因为要抛例外-点到红色线

https://ithelp.ithome.com.tw/upload/images/20220204/20119035RQuHQsXqBV.png

长这样
https://ithelp.ithome.com.tw/upload/images/20220204/20119035RS3EyVw1vY.png


package com.huang.myjson2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //宣告变数
    String json_string = "{\n" +
            "    title:JSONParserTutorial,\n" +
            "    array:[\n" +
            "    {\n" +
            "       company:Google\n" +
            "    },\n" +
            "    {\n" +
            "       company:Facebook\n" +
            "    },\n" +
            "    {\n" +
            "       company:LinkedIn\n" +
            "    },\n" +
            "    {\n" +
            "       company:Microsoft\n" +
            "    },\n" +
            "    {\n" +
            "       company:Apple\n" +
            "    },\n" +
            "    ],\n" +
            "\n" +
            "       nested:{\n" +
            "       flag:ture,\n" +
            "       random_number:1\n" +
            "\n" +
            "    }\n" +
            "}";

    ListView listView;
    ArrayList<String> items;

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

        //初始化
        listView = findViewById(R.id.list_view);
        items = new ArrayList<>();

        try {
            JSONObject object = new JSONObject(json_string);
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}


如何把它转到listView
https://ithelp.ithome.com.tw/upload/images/20220204/20119035oEq8N3ata2.png

package com.huang.myjson2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //宣告变数
    String json_string = "{\n" +
            "    title:JSONParserTutorial,\n" +
            "    array:[\n" +
            "    {\n" +
            "       company:Google\n" +
            "    },\n" +
            "    {\n" +
            "       company:Facebook\n" +
            "    },\n" +
            "    {\n" +
            "       company:LinkedIn\n" +
            "    },\n" +
            "    {\n" +
            "       company:Microsoft\n" +
            "    },\n" +
            "    {\n" +
            "       company:Apple\n" +
            "    },\n" +
            "    ],\n" +
            "\n" +
            "       nested:{\n" +
            "       flag:ture,\n" +
            "       random_number:1\n" +
            "\n" +
            "    }\n" +
            "}";

    ListView listView;
    ArrayList<String> items;

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

        //初始化
        listView = findViewById(R.id.list_view);
        items = new ArrayList<>();

        try {
            JSONObject object = new JSONObject(json_string);
            JSONArray array = object.getJSONArray("array");
            for(int i;i<array.length();i++){
                JSONObject o=array.getJSONObject(i);
                items.add(o.getString("company"));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        //把资料按照格式转好

    }
}

//把资料按照格式转好

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //宣告变数
    String json_string = "{       \n" +
            "\ttitle:JSONParserTutorial,       \n" +
            "\tarray:[       \n" +
            "  {       \n" +
            "\tcompany:Google           \n" +
            "  },       \n" +
            "  {       \n" +
            "\tcompany:Facebook           \n" +
            "  },       \n" +
            "  {       \n" +
            "\tcompany:LinkedIn           \n" +
            "  },       \n" +
            "  {       \n" +
            "\tcompany:Microsoft           \n" +
            "  },       \n" +
            "  {       \n" +
            "\tcompany:Apple           \n" +
            "  }       \n" +
            "  ],       \n" +
            "\tnested:{       \n" +
            "\tflag:true,       \n" +
            "\trandom_number:1       \n" +
            "  }       \n" +
            "}  ";
    ListView listView;
    ArrayList<String> items;

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

        listView = findViewById(R.id.list_view);
        items = new ArrayList<String>();

        try {
            JSONObject object = new JSONObject(json_string);
            JSONArray array = object.getJSONArray("array");
            for(int i=0; i<array.length(); i++){
                JSONObject o = array.getJSONObject(i);
                items.add(o.getString("company"));
            }
            //取得其他key的内容-----------------------
            JSONObject oo = object.getJSONObject("nested");
            String msg = oo.getString("flag");
            boolean msg2 = oo.getBoolean("flag");
            Toast.makeText(MainActivity.this, msg2+"", Toast.LENGTH_SHORT).show();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_list_item_1,
                items
        );
        listView.setAdapter(adapter);

    }
}

这里我的模拟器RUN不出来.
所以我自己上网找另外一个做看看

/images/emoticon/emoticon04.gif

後来发现是要用手机看-
https://ithelp.ithome.com.tw/upload/images/20220204/20119035twZD8dJvCm.jpg


所以我自己上网找另外一个做看看

java 档

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

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

        ListView listView=(ListView) findViewById(R.id.listview);

        String[] str ={"新北市","台北市","台中市","台南市","高雄市"};

        ArrayAdapter adapter =new ArrayAdapter(this,
                android.R.layout.simple_list_item_1,
                str);

        listView.setAdapter(adapter);
    }
}

xml档

<?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"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:id="@+id/listview" />

</LinearLayout>

也是用自己的手机测试

https://ithelp.ithome.com.tw/upload/images/20220204/20119035whLc6bkhkl.jpg


我先自己承认~因为下周要面试所以都在准备C#........
不过还是至少要来个纯文字版~
其实我觉得我写的Android除非是从0开始~
不然我觉得我写的小笔记蛮实用的0.0(老王卖瓜)
尤其是对英文不好的来说~

继续来写Android的XML的编辑功能:

  1. margin边界
  2. 内距padding=空多少空间不放字(按箭头往下填)
  3. 对齐gravity
  4. 按纽=toggleButton
  5. 按钮放上去之後=黄色!可以~ 红色!不行因为代表错误
  6. 要确认按钮4边至少要有一边有绑
  7. 按纽=Button (没有绑ID)
  8. 赋予功能使用的语法:toggleButton.setOnCheckedChangeListener();
  9. button一定会缩排-要有2个选项要选2个button
  10. Compontaint Tree预设是由上往下排

希望...可以让我继续有铁人发文的机会~拜托拜托


<<:  虹语岚访仲夏夜-24(专业的小四篇)

>>:  Day23 Gin with i18n

Day29 - Exploitation- Linux kernels 漏洞

Linux kernels 常有一些可以从一般使用者提权到 root 的漏洞,如 DirtyCOW ...

Day 26 密码规则定义规划实作

根据GDPR第5条和CCPA§§1798.83(d)(E)(iii) 和 §§1798.91.04(...

自动化测试,让你上班拥有一杯咖啡的时间 | Day 9 - 如何上传图片

此系列文章会同步发文到个人部落格,有兴趣的读者可以前往观看喔。 在 E2E 测试中,不仅有选取元素...

将传统 IPX/SPX 网路连接到 IP 网路,最合适的设备为闸道器

一个网路通过传输介质连接两个或多个节点,共享资源;它有两种架构视图:物理视图和逻辑视图。网路的逻辑...

Day 01 「科学 v.s. 数学」前言

笔者从业数年,面试过不少程序开发者。每当问到对方是否有做单元测试时,绝大多数的面试者总会说: 「我知...