又是JSON
開新專案
準備XML檔+ListView
放好ID
準備一個TXT檔案-
把txt檔案貼到java檔裡
先宣告變數-
TXT檔案貼上會自動解析成字串格式-
{
title:JSONParserTutorial,
array:[
{
company:Google
},
{
company:Facebook
},
{
company:LinkedIn
},
{
company:Microsoft
},
{
company:Apple
},
],
nested:{
flag:ture,
random_number:1
}
}
綠色都是要得~
宣告按鈕跟容器-
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);
//初始化
}
}
再來是初始化-
反紅是因為要拋例外-點到紅色線
長這樣
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
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不出來.
所以我自己上網找另外一個做看看
後來發現是要用手機看-
所以我自己上網找另外一個做看看
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>
也是用自己的手機測試
我先自己承認~因為下周要面試所以都在準備C#........
不過還是至少要來個純文字版~
其實我覺得我寫的Android除非是從0開始~
不然我覺得我寫的小筆記蠻實用的0.0(老王賣瓜)
尤其是對英文不好的來說~
繼續來寫Android的XML的編輯功能:
希望...可以讓我繼續有鐵人發文的機會~拜託拜託