Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. package com.example.lab2;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.graphics.Color;
  7. import android.os.Bundle;
  8. import android.text.Editable;
  9. import android.text.Layout;
  10. import android.text.TextWatcher;
  11. import android.util.Log;
  12. import android.view.KeyEvent;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.widget.EditText;
  16. import android.widget.ExpandableListAdapter;
  17. import android.widget.ExpandableListView;
  18. import android.widget.LinearLayout;
  19. import android.widget.TextView;
  20.  
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.LinkedList;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.StringTokenizer;
  27.  
  28. public class MainActivity extends AppCompatActivity {
  29.  
  30. ExpandableListView expandableListView;
  31. List<String> parents;
  32. Map<String, List<String>> children;
  33. ExpandableListAdapter listAdapter;
  34. String topText;
  35. Context context;
  36. int temp;
  37.  
  38.  
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42.  
  43. setContentView(R.layout.activity_main);
  44.  
  45.  
  46. final TextView childText = (TextView) findViewById(R.id.textChild);
  47. final EditText edittext = (EditText) findViewById(R.id.editText2);
  48. expandableListView = (ExpandableListView) findViewById(R.id.simpleExpandableListView);
  49. topText = "/";
  50. fillData();
  51.  
  52. listAdapter = new MyExpandableListAdapter(this, parents, children);
  53.  
  54. expandableListView.setAdapter(listAdapter);
  55.  
  56.  
  57.  
  58. expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
  59. @Override
  60. public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
  61. if(!parent.isGroupExpanded(groupPosition)){
  62. parent.expandGroup(groupPosition);
  63. edittext.setText("/" + parents.get(groupPosition));
  64. } else if(parent.isGroupExpanded(groupPosition)){
  65. parent.collapseGroup(groupPosition);
  66. edittext.setText("/");
  67. }
  68. return false;
  69. }
  70. });
  71.  
  72.  
  73. expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  74. @Override
  75. public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
  76. topText = ("/" + parents.get(groupPosition) + "/" + children.get(parents.get(groupPosition)).get(childPosition));
  77. edittext.setText(topText);
  78.  
  79.  
  80. return false;
  81. }
  82. });
  83.  
  84.  
  85. edittext.setOnKeyListener(new View.OnKeyListener() {
  86. @Override
  87. public boolean onKey(View v, int keyCode, KeyEvent event) {
  88.  
  89.  
  90. String abc = edittext.getText().toString();
  91. StringTokenizer st = new StringTokenizer(abc, "/");
  92. LinkedList<String> names = new LinkedList<>();
  93. int counter = 0;
  94.  
  95. while (counter < 2 && st.hasMoreTokens()) {
  96. names.add(st.nextToken());
  97. counter++;
  98. }
  99.  
  100. boolean con = true;
  101.  
  102. for (int i = 0; con && i < parents.size(); i++) {
  103. if (names.size() > 0 && parents.get(i).startsWith(names.get(0)) || names.size() == 0 && abc.equals("/")) {
  104. con = false;
  105.  
  106. edittext.setBackgroundColor(Color.WHITE);
  107. expandableListView.expandGroup(i);
  108. boolean com = true;
  109. for (int j = 0; com && j < 3; j++) {
  110. if (names.size() > 1 && children.get(parents.get(i)).get(j).startsWith(names.get(1)) || names.size() == 1 && abc.equals(parents.get(i) + "/")) {
  111.  
  112. com = false;
  113.  
  114. expandableListView.getChildAt(temp).setBackgroundColor(Color.WHITE);
  115. int index = expandableListView.getFlatListPosition(ExpandableListView.getPackedPositionForChild(i, j));
  116. expandableListView.getChildAt(index).setBackgroundColor(Color.GRAY);
  117. temp = index;
  118. }
  119.  
  120. }
  121. if (com && names.size() > 1) {
  122. edittext.setBackgroundColor(Color.RED);
  123. }
  124. } else {
  125. edittext.setBackgroundColor(Color.RED);
  126.  
  127. }
  128.  
  129. }
  130. return false;
  131. }
  132.  
  133. });
  134.  
  135. }
  136. public void fillData() {
  137.  
  138. parents = new ArrayList<>();
  139. children = new HashMap<>();
  140.  
  141. parents.add("Brands");
  142. parents.add("Models");
  143. parents.add("Colors");
  144.  
  145. List<String> Brands = new ArrayList<>();
  146. List<String> Models = new ArrayList<>();
  147. List<String> Colors = new ArrayList<>();
  148.  
  149. Brands.add("Gucci");
  150. Brands.add("Prada");
  151. Brands.add("Tommy Hilfiger");
  152.  
  153.  
  154. Models.add("Round");
  155. Models.add("Squared");
  156. Models.add("Cat");
  157.  
  158. Colors.add("Red");
  159. Colors.add("Black");
  160. Colors.add("Blue");
  161.  
  162. children.put(parents.get(0), Brands);
  163. children.put(parents.get(1), Models);
  164. children.put(parents.get(2), Colors);
  165.  
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement