Guest User

Untitled

a guest
May 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.54 KB | None | 0 0
  1. import android.app.Fragment;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.support.annotation.Nullable;
  5. import android.view.LayoutInflater;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.Button;
  11. import android.widget.LinearLayout;
  12. import android.widget.PopupMenu;
  13. import android.widget.TextView;
  14.  
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.List;
  18.  
  19. public class FragmentDatabase extends Fragment{
  20.  
  21. private final String LOG_TAG = "FRAGMENTDATABASE";
  22.  
  23. DatabaseListener listener;
  24.  
  25. int rowHeight = 40;
  26. String text;
  27.  
  28. Button select;
  29. TextView counter;
  30. LinearLayout titleBar;
  31. LinearLayout dataList;
  32. PopupMenu popup;
  33.  
  34. String[] fields;
  35. int[] colWidths;
  36. List<String> data;
  37. HashMap<Integer, String[]> dataItems;
  38. HashMap<Integer, String> menuItems;
  39.  
  40. DBAdapter db;
  41. public interface DatabaseListener{
  42. void onload(String table);
  43. }
  44.  
  45. @Nullable
  46. @Override
  47. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  48. super.onCreateView(inflater, container, savedInstanceState);
  49. View v = inflater.inflate(R.layout.fragment_database, container, false);
  50. select = (Button)v.findViewById(R.id.select);
  51. counter = (TextView)v.findViewById(R.id.count);
  52. titleBar = (LinearLayout)v.findViewById(R.id.titles);
  53. dataList = (LinearLayout) v.findViewById(R.id.list);
  54.  
  55. data = new ArrayList<>();
  56. menuItems = new HashMap<>();
  57. dataItems = new HashMap<>();
  58.  
  59. db = new DBAdapter(getActivity().getApplicationContext());
  60. data = db.allTables();
  61.  
  62. popup = new PopupMenu(getActivity().getApplicationContext(), select);
  63. for(int i = 0; i < data.size(); i++) {
  64. popup.getMenu().add(Menu.NONE, i, i, data.get(i));
  65. menuItems.put(i, data.get(i));
  66. }
  67.  
  68. popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
  69. @Override
  70. public boolean onMenuItemClick(MenuItem menuItem) {
  71. dataList.removeAllViews();
  72. titleBar.removeAllViews();
  73. dataItems.clear();
  74.  
  75. int itemId = menuItem.getItemId();
  76. String table = menuItems.get(itemId);
  77. fields = db.allFields(table);
  78. TextView idField = new TextView(getActivity().getApplicationContext());
  79. idField.setText(fields[0]);
  80. titleBar.addView(idField);
  81. colWidths = new int[fields.length];
  82. colWidths[0] = 80;
  83. for(int i = 1; i < fields.length; i++){
  84. colWidths[i] = 200;
  85. TextView f = new TextView(getActivity().getApplicationContext());
  86. String text = fields[i];
  87. f.setText(text);
  88. titleBar.addView(f);
  89. }
  90.  
  91. dataItems = db.allValues(table);
  92. counter.setText(String.valueOf(dataItems.size()));
  93.  
  94. for(int i = 0; i < dataItems.size(); i++){
  95.  
  96. LinearLayout row = new LinearLayout(getActivity().getApplicationContext());
  97. row.setOrientation(LinearLayout.HORIZONTAL);
  98.  
  99. String[] rowData = dataItems.get(i);
  100.  
  101. TextView idf = new TextView(getActivity().getApplicationContext());
  102. if(rowData.length != 0) idf.setText(rowData[0]);
  103. row.addView(idf);
  104.  
  105. for(int j = 1; j < rowData.length; j++){
  106. TextView idf2 = new TextView(getActivity().getApplicationContext());
  107. text = rowData[j];
  108. row.addView(idf2);
  109.  
  110. if(text != null && text.length() > 9){
  111. int length = 18 * text.length();
  112. if(colWidths[j] < length) colWidths[j] = length;
  113. }
  114. idf2.setText(text);
  115. }
  116. dataList.addView(row);
  117. }
  118.  
  119. for(int j = 0; j < colWidths.length; j++){
  120. TextView tv = (TextView) titleBar.getChildAt(j);
  121. ViewGroup.LayoutParams vlp = tv.getLayoutParams();
  122. vlp.width = colWidths[j];
  123. vlp.height = rowHeight;
  124. tv.setLayoutParams(vlp);
  125. }
  126. for(int i = 0; i < dataItems.size(); i++){
  127. LinearLayout rowLayout = (LinearLayout) dataList.getChildAt(i);
  128. for(int j = 0; j < colWidths.length; j++){
  129. TextView tv = (TextView) rowLayout.getChildAt(j);
  130. ViewGroup.LayoutParams vlp = tv.getLayoutParams();
  131. vlp.width = colWidths[j];
  132. vlp.height = rowHeight;
  133. tv.setLayoutParams(vlp);
  134. }
  135. }
  136. return true;
  137. }
  138. });
  139.  
  140. select.setOnClickListener(new View.OnClickListener() {
  141. @Override
  142. public void onClick(View view) {
  143. popup.show();
  144. }
  145. });
  146. return v;
  147. }
  148.  
  149. @Override
  150. public void onAttach(Context context) {
  151. super.onAttach(context);
  152. if (context instanceof FragmentDatabase.DatabaseListener) {
  153. listener = (FragmentDatabase.DatabaseListener) context;
  154. } else {
  155. throw new RuntimeException(context.toString()
  156. + " must implement FragmentListener");
  157. }
  158. }
  159.  
  160. @Override
  161. public void onDetach() {
  162. listener = null;
  163. super.onDetach();
  164. }
  165. }
  166.  
  167. myDbHelper myhelper;
  168.  
  169. public List<String> allTables(){
  170. List<String> result = new ArrayList<>();
  171. SQLiteDatabase db = myhelper.getWritableDatabase();
  172. Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type = 'table'",null);
  173. if(c.moveToFirst()){
  174. while (!c.isAfterLast()){
  175. result.add(c.getString(0));
  176. c.moveToNext();
  177. }
  178. }
  179. return result;
  180. }
  181.  
  182. public String[] allFields(String table){
  183. List<String> result = new ArrayList<>();
  184. SQLiteDatabase db = myhelper.getWritableDatabase();
  185. Cursor c = db.query(table, null, null, null, null, null, null);
  186. return c.getColumnNames();
  187. }
  188.  
  189. public HashMap<Integer, String[]> allValues(String table) {
  190. HashMap<Integer, String[]> result = new HashMap<>();
  191. String[] row;
  192.  
  193. SQLiteDatabase db = myhelper.getWritableDatabase();
  194. Cursor c = db.rawQuery("SELECT * FROM "+table, null);
  195. int columns = c.getColumnCount();
  196. if(c.moveToFirst()){
  197. int rowCount = 0;
  198. while(!c.isAfterLast()){
  199. row = new String[columns];
  200. for(int i = 0; i < columns; i++){
  201. row[i] = c.getString(i);
  202. Log.i(LOG_TAG, "Row add "+String.valueOf(c.getString(i)));
  203. }
  204. result.put(rowCount, row);
  205. rowCount ++;
  206. c.moveToNext();
  207. }
  208. }
  209. return result;
  210. }
  211.  
  212. static class myDbHelper extends SQLiteOpenHelper
  213. {
  214. private Context context;
  215.  
  216. public myDbHelper(Context context) {
  217. super(context, DATABASE_NAME, null, DATABASE_Version);
  218. this.context=context;
  219. onCreate(this.getWritableDatabase());
  220. }
  221. }
  222. }
  223.  
  224. <?xml version="1.0" encoding="utf-8"?>
  225. <RelativeLayout
  226. xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
  227. android:layout_height="match_parent">
  228. <LinearLayout
  229. android:id="@+id/top_bar"
  230. android:layout_width="match_parent"
  231. android:layout_height="48dp"
  232. android:background="#888888"
  233. android:orientation="horizontal"
  234. >
  235. <Button
  236. android:id="@+id/select"
  237. android:layout_width="100dp"
  238. android:layout_height="match_parent"
  239. android:text="table"/>
  240. <TextView
  241. android:id="@+id/count"
  242. android:layout_width="100dp"
  243. android:layout_height="match_parent"
  244. android:text="total"
  245. android:textSize="24sp"
  246. android:gravity="center_horizontal"
  247. android:layout_marginTop="5dp"/>
  248. </LinearLayout>
  249. <HorizontalScrollView
  250. android:layout_below="@+id/top_bar"
  251. android:layout_width="match_parent"
  252. android:layout_height="match_parent">
  253. <LinearLayout
  254. android:layout_width="match_parent"
  255. android:layout_height="match_parent"
  256. android:orientation="vertical"
  257. >
  258. <LinearLayout
  259. android:id="@+id/titles"
  260. android:background="#777777"
  261. android:layout_width="match_parent"
  262. android:layout_height="36dp"
  263. android:orientation="horizontal">
  264. </LinearLayout>
  265. <ScrollView
  266. android:layout_width="match_parent"
  267. android:layout_height="match_parent">
  268. <LinearLayout
  269. android:id="@+id/list"
  270. android:orientation="vertical"
  271. android:layout_width="match_parent"
  272. android:layout_height="0dp"
  273. android:layout_weight="1">
  274.  
  275. </LinearLayout>
  276. </ScrollView>
  277. </LinearLayout>
  278. </HorizontalScrollView>
  279. </RelativeLayout>
Add Comment
Please, Sign In to add comment