1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | public class MainActivity extends AppCompatActivity { private static final int[] BUTTON_IDS = { R.id.btn11, R.id.btn12, R.id.btn13, R.id.btn14, R.id.btn15, R.id.btn16, R.id.btn17, R.id.btn18, R.id.btn21, R.id.btn22, R.id.btn23, R.id.btn24, R.id.btn25, R.id.btn26, R.id.btn27, R.id.btn28, R.id.btn31, R.id.btn32, R.id.btn33, R.id.btn34, R.id.btn35, R.id.btn36, R.id.btn37, R.id.btn38, R.id.btn41, R.id.btn42, R.id.btn43, R.id.btn44, R.id.btn45, R.id.btn46, R.id.btn47, R.id.btn48, R.id.btn51, R.id.btn52, R.id.btn53, R.id.btn54, R.id.btn55, R.id.btn56, R.id.btn57, R.id.btn58, R.id.btn61, R.id.btn62, R.id.btn63, R.id.btn64, R.id.btn65, R.id.btn66, R.id.btn67, R.id.btn68, R.id.btn71, R.id.btn72, R.id.btn73, R.id.btn74, R.id.btn75, R.id.btn76, R.id.btn77, R.id.btn78, R.id.btn81, R.id.btn82, R.id.btn83, R.id.btn84, R.id.btn85, R.id.btn86, R.id.btn87, R.id.btn88 }; private static final String TAG = "XML"; private List<Button> buttons; GridLayout gridLayout; Button btn[][] = new Button[8][8]; Point point = new Point(); int pointNo = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(SCREEN_ORIENTATION_UNSPECIFIED); setContentView(R.layout.activity_main); gridLayout = findViewById(R.id.gridLayout); buttons = new ArrayList<>(BUTTON_IDS.length); for (int id : BUTTON_IDS) { Button button = findViewById(id); buttons.add(button); } makeButtonMatrix(); //p1.setPosition("77"); //xmlから ex> code=22 をもらう際に(1~7のみ) int count = getCountByXml(); for(pointNo =1; pointNo<=4; pointNo++) { makePointLocation(); } } private void makePointLocation() { point = parseXml(pointNo); //1~16個の給油ポイントの情報を保存のため Log.i("TAG","point: " + point.toString()); if(point != null) { setPointLocation(point); } } public Point parseXml(int pointNo) { Log.i(TAG, "parser()"); ArrayList<Point> arrayList = new ArrayList<>(); //内部XMLファイル使う際に InputStream inputStream = getResources().openRawResource(R.raw.sample); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(inputStreamReader); Point p = new Point(); try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlParser = factory.newPullParser(); xmlParser.setInput(reader); String tag; // TAGを保存のため //StringBuffer attr = new StringBuffer(); //Stringを保存と出力のため int eventType = xmlParser.getEventType(); //END TAGじゃないと Loopを回しながらParsingになる while (eventType != END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: tag = xmlParser.getName(); //現タグ名を保存 if (tag.equals("add")) { //nameであるタグが入ると Toast.makeText(getApplicationContext(), "value", Toast.LENGTH_LONG).show(); if (xmlParser.getAttributeValue(0).equals("給油ポイント" + pointNo + " 表示位置")) { p.setPosition(xmlParser.getAttributeValue(1)); } else if (xmlParser.getAttributeValue(0).equals("給油ポイント" + pointNo + " 表示名")) { p.setPointNum(xmlParser.getAttributeValue(1)); } else break; } break; }//End of switch eventType = xmlParser.next(); } }catch (IOException | XmlPullParserException e) { e.printStackTrace(); Log.i("TAG","exception"); } return p; } public int getCountByXml() { Log.i(TAG, "parser()"); ArrayList<Point> arrayList = new ArrayList<>(); //内部XMLファイル使う際に InputStream inputStream = getResources().openRawResource(R.raw.sample); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(inputStreamReader); int pCount=0; try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlParser = factory.newPullParser(); xmlParser.setInput(reader); String tag; // TAGを保存のため //StringBuffer attr = new StringBuffer(); //Stringを保存と出力のため int eventType = xmlParser.getEventType(); //END TAGじゃないと Loopを回しながらParsingになる while (eventType != END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: tag = xmlParser.getName(); //現タグ名を保存 if (tag.equals("add")) { //nameであるタグが入ると for (int index = 0; index < xmlParser.getAttributeCount(); index++) { int code = index + 1; if (xmlParser.getAttributeValue(index).equals("通信台数")) { pCount = Integer.parseInt(xmlParser.getAttributeValue(code)); } } } break; }//End of switch eventType = xmlParser.next(); } }catch (Exception e) { e.printStackTrace(); Log.i("TAG","exception"); } return pCount; } //Buttonの 2x2行列化 public void makeButtonMatrix() { int idx=0; for(int i=0; i<8; i++){ for(int j=0; j<8; j++){ btn[i][j] = findViewById(R.id.btn11+idx); idx++; } } } //ポイント位置の初期設定のため public void setPointLocation(Point p){ int row = Integer.parseInt(p.getPosition()) % 10 - 1; int col = Integer.parseInt(p.getPosition()) / 10 - 1; //表示位置デザイン View grid = findViewById(btn[row][col].getId()); GridLayout.LayoutParams params = new GridLayout.LayoutParams(grid.getLayoutParams()); params.rowSpec = GridLayout.spec(row,2); params.columnSpec = GridLayout.spec(col,2); params.setGravity(Gravity.FILL); grid.setLayoutParams(params); grid.setVisibility(View.VISIBLE); //表示名デザイン Button mBtn = btn[row][col]; mBtn.setTextColor(Color.WHITE); mBtn.setText(p.getPointNum()); mBtn.setGravity(Gravity.LEFT|Gravity.TOP); mBtn.setTextSize(32); mBtn.setTypeface(null, Typeface.BOLD); mBtn.setPadding(15,0,0,0); mBtn.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); } } | cs |
'Android' 카테고리의 다른 글
멀티스레딩 이슈 - Atomic (0) | 2020.07.15 |
---|---|
2차 작성(파싱함수 수정) (0) | 2020.07.15 |
XML Parsing (0) | 2020.07.08 |
그리드레이아웃의 자식 뷰(버튼)의 속성(span)변경 (0) | 2020.07.08 |
레이아웃 작성 레퍼런스 (0) | 2020.07.03 |
댓글