Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.59 KB | None | 0 0
  1. public class SecondOptionNotices extends Fragment implements Response.Listener<JSONObject>, Response.ErrorListener{
  2.  
  3. View view;
  4. ViewGroup layout;
  5.  
  6. private ProgressDialog progressDialog;
  7.  
  8. LinearLayout contentLayouts;
  9. LinearLayout linearLayout;
  10. LayoutInflater inflater;
  11. SwipeRefreshLayout swipeRefreshLayout;
  12. ScrollView scrollViewNotices;
  13. LinearLayout loadBottom;
  14. RequestQueue requestQueue;
  15. JsonObjectRequest jsonObjectRequest;
  16. TextView nombre, fecha, titulo, texto;
  17. ImageView img1, img2, img3, img4, img5, img6;
  18. FloatingActionButton btnFloatingPost;
  19.  
  20. int checkBottom = 0;
  21.  
  22. public interface UpdateNotices{
  23. public void updateViews();
  24. }
  25.  
  26. UpdateNotices updateViewNotices;
  27.  
  28. public static SecondOptionNotices newInstance(){
  29. return new SecondOptionNotices();
  30. }
  31.  
  32. @Override
  33. public void onAttach(Context context) {
  34. super.onAttach(context);
  35. try {
  36. updateViewNotices = (UpdateNotices) context;
  37. }catch (ClassCastException e){
  38. throw new ClassCastException(context.toString() + " Debe implementar la interfaz Update en su Activity");
  39. }
  40. }
  41.  
  42. @Nullable
  43. @Override
  44. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  45. view = inflater.inflate(R.layout.second_option_notices, container, false);
  46.  
  47. return view;
  48. }
  49.  
  50. @Override
  51. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  52. super.onViewCreated(view, savedInstanceState);
  53.  
  54. init();
  55. }
  56.  
  57. private void init() {
  58. inflater = LayoutInflater.from(getContext());
  59. contentLayouts = view.findViewById(R.id.content_layouts);
  60. swipeRefreshLayout = view.findViewById(R.id.swipe_notices);
  61. scrollViewNotices = view.findViewById(R.id.scrollview_notices);
  62. requestQueue = Volley.newRequestQueue(Objects.requireNonNull(getContext()));
  63. btnFloatingPost = view.findViewById(R.id.btn_floating_notices);
  64. addLayouts();
  65. swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  66. @Override
  67. public void onRefresh() {
  68. deleteNoticesRefresh();
  69. loadWebService();
  70. swipeRefreshLayout.setRefreshing(false);
  71. }
  72. });
  73.  
  74. btnFloatingPost.setOnClickListener(new View.OnClickListener() {
  75. @Override
  76. public void onClick(View v) {
  77. updateViewNotices.updateViews();
  78. }
  79. });
  80.  
  81. scrollViewNotices.getViewTreeObserver()
  82. .addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
  83. @Override
  84. public void onScrollChanged() {
  85. if(scrollViewNotices.getChildAt(0).getBottom() <= (scrollViewNotices.getHeight() + scrollViewNotices.getScrollY())){
  86. addLayouts();
  87. }
  88. }
  89. });
  90. }
  91.  
  92. private void deleteNoticesRefresh(){
  93. if(layout.getChildCount() > 0){
  94. layout.removeAllViews();
  95. }
  96. }
  97.  
  98. private void loadWebService() {
  99. progressDialog = new ProgressDialog(getContext());
  100. progressDialog.setMessage("Cargando...");
  101. progressDialog.show();
  102.  
  103. String ip = getString(R.string.ip);
  104. String url = ip + "/firstTestSmb/wsConsultarList.php";
  105.  
  106. jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
  107. try{
  108. requestQueue.add(jsonObjectRequest);
  109. }catch (NullPointerException n){
  110. n.printStackTrace();
  111. Log.i("ERROR_NULL", n.toString());
  112. }
  113. }
  114.  
  115. @Override
  116. public void onResponse(JSONObject response) {
  117. //Notices notices = null;
  118. JSONArray jsonArray = response.optJSONArray("noticias");
  119.  
  120. try {
  121. for(int i = 0; i < jsonArray.length(); i++){
  122. //notices = new Notices();
  123. JSONObject jsonObject = null;
  124. jsonObject = jsonArray.getJSONObject(i);
  125.  
  126. String datos[] = {jsonObject.optString("nombre"),
  127. jsonObject.optString("titulo"),
  128. jsonObject.optString("fecha"),
  129. jsonObject.optString("texto"),
  130. jsonObject.optString("url1"),
  131. jsonObject.optString("url2"),
  132. jsonObject.optString("url3"),
  133. jsonObject.optString("url4"),
  134. jsonObject.optString("url5"),
  135. jsonObject.optString("url6")};
  136.  
  137. addObject(datos);
  138. }
  139. progressDialog.hide();
  140.  
  141. } catch (JSONException e) {
  142. progressDialog.hide();
  143. e.printStackTrace();
  144. Toast.makeText(getContext(), "No se ha podido establecer conexion con el servidor" + " " + response, Toast.LENGTH_SHORT).show();
  145. Log.i("ERROR_ARRAY", response.toString());
  146. }
  147. }
  148.  
  149. @Override
  150. public void onErrorResponse(VolleyError error) {
  151. progressDialog.hide();
  152. Toast.makeText(getContext(), "No se Ha Podido Conectar", Toast.LENGTH_SHORT).show();
  153. Log.i("ERROR_CONEXION", error.toString());
  154. }
  155.  
  156. private void addLayouts(){
  157. checkBottom = 1;
  158. linearLayout = (LinearLayout) inflater.inflate(R.layout.layouts_notices, null, false);
  159. contentLayouts.addView(linearLayout);
  160. loadBottom = linearLayout.findViewById(R.id.load_bottom);
  161. loadBottom.setVisibility(View.VISIBLE);
  162. layout = linearLayout.findViewById(R.id.contentObj);
  163. loadWebService();
  164. loadBottom.setVisibility(View.GONE);
  165. }
  166.  
  167. private void addObject(String datos[]) {
  168. String urlsImages[] = {
  169. datos[4],
  170. datos[5],
  171. datos[6],
  172. datos[7],
  173. datos[8],
  174. datos[9]
  175. };
  176.  
  177. if(urlsImages[0].equalsIgnoreCase("No Registra") &&
  178. urlsImages[1].equalsIgnoreCase("No Registra") &&
  179. urlsImages[2].equalsIgnoreCase("No Registra") &&
  180. urlsImages[3].equalsIgnoreCase("No Registra") &&
  181. urlsImages[4].equalsIgnoreCase("No Registra") &&
  182. urlsImages[5].equalsIgnoreCase("No Registra")){
  183.  
  184. RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_dates, null, false);
  185.  
  186. nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
  187. fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
  188. titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
  189. texto = relativeLayout.findViewById(R.id.texto_opt_notices);
  190.  
  191. nombre.setText(datos[0]);
  192. fecha.setText(datos[1]);
  193. titulo.setText(datos[2]);
  194. texto.setText(datos[3]);
  195.  
  196. layout.addView(relativeLayout);
  197.  
  198. }else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
  199. urlsImages[1].equalsIgnoreCase("No Registra") &&
  200. urlsImages[2].equalsIgnoreCase("No Registra") &&
  201. urlsImages[3].equalsIgnoreCase("No Registra") &&
  202. urlsImages[4].equalsIgnoreCase("No Registra") &&
  203. urlsImages[5].equalsIgnoreCase("No Registra")){
  204.  
  205. RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_one_image, null, false);
  206.  
  207. nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
  208. fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
  209. titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
  210. texto = relativeLayout.findViewById(R.id.texto_opt_notices);
  211. img1 = relativeLayout.findViewById(R.id.image_alone_card);
  212.  
  213. nombre.setText(datos[0]);
  214. fecha.setText(datos[1]);
  215. titulo.setText(datos[2]);
  216. texto.setText(datos[3]);
  217. loadImages(0, urlsImages);
  218.  
  219. layout.addView(relativeLayout);
  220.  
  221. }else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
  222. !urlsImages[1].equalsIgnoreCase("No Registra") &&
  223. urlsImages[2].equalsIgnoreCase("No Registra") &&
  224. urlsImages[3].equalsIgnoreCase("No Registra") &&
  225. urlsImages[4].equalsIgnoreCase("No Registra") &&
  226. urlsImages[5].equalsIgnoreCase("No Registra")){
  227.  
  228. RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_two_image, null, false);
  229.  
  230. nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
  231. fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
  232. titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
  233. texto = relativeLayout.findViewById(R.id.texto_opt_notices);
  234. img1 = relativeLayout.findViewById(R.id.image_alone_card);
  235. img2 = relativeLayout.findViewById(R.id.image_two_card);
  236.  
  237. nombre.setText(datos[0]);
  238. fecha.setText(datos[1]);
  239. titulo.setText(datos[2]);
  240. texto.setText(datos[3]);
  241. loadImages(1, urlsImages);
  242.  
  243. layout.addView(relativeLayout);
  244.  
  245. }else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
  246. !urlsImages[1].equalsIgnoreCase("No Registra") &&
  247. !urlsImages[2].equalsIgnoreCase("No Registra") &&
  248. urlsImages[3].equalsIgnoreCase("No Registra") &&
  249. urlsImages[4].equalsIgnoreCase("No Registra") &&
  250. urlsImages[5].equalsIgnoreCase("No Registra")){
  251.  
  252. RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_tree_image, null, false);
  253.  
  254. nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
  255. fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
  256. titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
  257. texto = relativeLayout.findViewById(R.id.texto_opt_notices);
  258. img1 = relativeLayout.findViewById(R.id.image_alone_card);
  259. img2 = relativeLayout.findViewById(R.id.image_two_card);
  260. img3 = relativeLayout.findViewById(R.id.image_tree_card);
  261.  
  262. nombre.setText(datos[0]);
  263. fecha.setText(datos[1]);
  264. titulo.setText(datos[2]);
  265. texto.setText(datos[3]);
  266. loadImages(2, urlsImages);
  267.  
  268. layout.addView(relativeLayout);
  269.  
  270. }else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
  271. !urlsImages[1].equalsIgnoreCase("No Registra") &&
  272. !urlsImages[2].equalsIgnoreCase("No Registra") &&
  273. !urlsImages[3].equalsIgnoreCase("No Registra") &&
  274. urlsImages[4].equalsIgnoreCase("No Registra") &&
  275. urlsImages[5].equalsIgnoreCase("No Registra")){
  276.  
  277. RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_four_image, null, false);
  278.  
  279. nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
  280. fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
  281. titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
  282. texto = relativeLayout.findViewById(R.id.texto_opt_notices);
  283. img1 = relativeLayout.findViewById(R.id.image_alone_card);
  284. img2 = relativeLayout.findViewById(R.id.image_two_card);
  285. img3 = relativeLayout.findViewById(R.id.image_tree_card);
  286. img4 = relativeLayout.findViewById(R.id.image_four_card);
  287.  
  288. nombre.setText(datos[0]);
  289. fecha.setText(datos[1]);
  290. titulo.setText(datos[2]);
  291. texto.setText(datos[3]);
  292. loadImages(3, urlsImages);
  293.  
  294. layout.addView(relativeLayout);
  295.  
  296. }else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
  297. !urlsImages[1].equalsIgnoreCase("No Registra") &&
  298. !urlsImages[2].equalsIgnoreCase("No Registra") &&
  299. !urlsImages[3].equalsIgnoreCase("No Registra") &&
  300. !urlsImages[4].equalsIgnoreCase("No Registra") &&
  301. urlsImages[5].equalsIgnoreCase("No Registra")){
  302.  
  303. RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_five_image, null, false);
  304.  
  305. nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
  306. fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
  307. titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
  308. texto = relativeLayout.findViewById(R.id.texto_opt_notices);
  309. img1 = relativeLayout.findViewById(R.id.image_alone_card);
  310. img2 = relativeLayout.findViewById(R.id.image_two_card);
  311. img3 = relativeLayout.findViewById(R.id.image_tree_card);
  312. img4 = relativeLayout.findViewById(R.id.image_four_card);
  313. img5 = relativeLayout.findViewById(R.id.image_five_card);
  314.  
  315. nombre.setText(datos[0]);
  316. fecha.setText(datos[1]);
  317. titulo.setText(datos[2]);
  318. texto.setText(datos[3]);
  319. loadImages(4, urlsImages);
  320.  
  321. layout.addView(relativeLayout);
  322.  
  323. }else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
  324. !urlsImages[1].equalsIgnoreCase("No Registra") &&
  325. !urlsImages[2].equalsIgnoreCase("No Registra") &&
  326. !urlsImages[3].equalsIgnoreCase("No Registra") &&
  327. !urlsImages[4].equalsIgnoreCase("No Registra") &&
  328. !urlsImages[5].equalsIgnoreCase("No Registra")){
  329.  
  330. RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_six_image, null, false);
  331.  
  332. nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
  333. fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
  334. titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
  335. texto = relativeLayout.findViewById(R.id.texto_opt_notices);
  336. img1 = relativeLayout.findViewById(R.id.image_alone_card);
  337. img2 = relativeLayout.findViewById(R.id.image_two_card);
  338. img3 = relativeLayout.findViewById(R.id.image_tree_card);
  339. img4 = relativeLayout.findViewById(R.id.image_four_card);
  340. img5 = relativeLayout.findViewById(R.id.image_five_card);
  341. img6 = relativeLayout.findViewById(R.id.image_six_card);
  342.  
  343. nombre.setText(datos[0]);
  344. fecha.setText(datos[1]);
  345. titulo.setText(datos[2]);
  346. texto.setText(datos[3]);
  347. loadImages(5, urlsImages);
  348.  
  349. layout.addView(relativeLayout);
  350.  
  351. }
  352. }
  353.  
  354. private void loadImages(int id, String urls[]){
  355. switch (id){
  356. case 0:
  357. Glide.with(getContext())
  358. .load(urls[0])
  359. .centerCrop()
  360. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  361. .skipMemoryCache(true)
  362. .thumbnail(0.1f)
  363. .into(img1);
  364. break;
  365. case 1:
  366. Glide.with(getContext())
  367. .load(urls[0])
  368. .centerCrop()
  369. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  370. .skipMemoryCache(true)
  371. .thumbnail(0.1f)
  372. .into(img1);
  373. Glide.with(getContext())
  374. .load(urls[1])
  375. .centerCrop()
  376. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  377. .skipMemoryCache(true)
  378. .thumbnail(0.1f)
  379. .into(img2);
  380. break;
  381. case 2:
  382. Glide.with(getContext())
  383. .load(urls[0])
  384. .centerCrop()
  385. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  386. .skipMemoryCache(true)
  387. .thumbnail(0.1f)
  388. .into(img1);
  389. Glide.with(getContext())
  390. .load(urls[1])
  391. .centerCrop()
  392. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  393. .skipMemoryCache(true)
  394. .thumbnail(0.1f)
  395. .into(img2);
  396. Glide.with(getContext())
  397. .load(urls[2])
  398. .centerCrop()
  399. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  400. .skipMemoryCache(true)
  401. .thumbnail(0.1f)
  402. .into(img3);
  403. break;
  404. case 3:
  405. Glide.with(getContext())
  406. .load(urls[0])
  407. .centerCrop()
  408. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  409. .skipMemoryCache(true)
  410. .thumbnail(0.1f)
  411. .into(img1);
  412. Glide.with(getContext())
  413. .load(urls[1])
  414. .centerCrop()
  415. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  416. .skipMemoryCache(true)
  417. .thumbnail(0.1f)
  418. .into(img2);
  419. Glide.with(getContext())
  420. .load(urls[2])
  421. .centerCrop()
  422. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  423. .skipMemoryCache(true)
  424. .thumbnail(0.1f)
  425. .into(img3);
  426. Glide.with(getContext())
  427. .load(urls[3])
  428. .centerCrop()
  429. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  430. .skipMemoryCache(true)
  431. .thumbnail(0.1f)
  432. .into(img4);
  433. break;
  434. case 4:
  435. Glide.with(getContext())
  436. .load(urls[0])
  437. .centerCrop()
  438. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  439. .skipMemoryCache(true)
  440. .thumbnail(0.1f)
  441. .into(img1);
  442. Glide.with(getContext())
  443. .load(urls[1])
  444. .centerCrop()
  445. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  446. .skipMemoryCache(true)
  447. .thumbnail(0.1f)
  448. .into(img2);
  449. Glide.with(getContext())
  450. .load(urls[2])
  451. .centerCrop()
  452. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  453. .skipMemoryCache(true)
  454. .thumbnail(0.1f)
  455. .into(img3);
  456. Glide.with(getContext())
  457. .load(urls[3])
  458. .centerCrop()
  459. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  460. .skipMemoryCache(true)
  461. .thumbnail(0.1f)
  462. .into(img4);
  463. Glide.with(getContext())
  464. .load(urls[4])
  465. .centerCrop()
  466. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  467. .skipMemoryCache(true)
  468. .thumbnail(0.1f)
  469. .into(img5);
  470. break;
  471. case 5:
  472. Glide.with(getContext())
  473. .load(urls[0])
  474. .centerCrop()
  475. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  476. .skipMemoryCache(true)
  477. .thumbnail(0.1f)
  478. .into(img1);
  479. Glide.with(getContext())
  480. .load(urls[1])
  481. .centerCrop()
  482. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  483. .skipMemoryCache(true)
  484. .thumbnail(0.1f)
  485. .into(img2);
  486. Glide.with(getContext())
  487. .load(urls[2])
  488. .centerCrop()
  489. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  490. .skipMemoryCache(true)
  491. .thumbnail(0.1f)
  492. .into(img3);
  493. Glide.with(getContext())
  494. .load(urls[3])
  495. .centerCrop()
  496. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  497. .skipMemoryCache(true)
  498. .thumbnail(0.1f)
  499. .into(img4);
  500. Glide.with(getContext())
  501. .load(urls[4])
  502. .centerCrop()
  503. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  504. .skipMemoryCache(true)
  505. .thumbnail(0.1f)
  506. .into(img5);
  507. Glide.with(getContext())
  508. .load(urls[5])
  509. .centerCrop()
  510. .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  511. .skipMemoryCache(true)
  512. .thumbnail(0.1f)
  513. .into(img6);
  514. break;
  515. }
  516. }
  517. }
  518.  
  519. <?PHP
  520.  
  521. $hostname_localhost="localhost";
  522. $database_localhost="db_test_first";
  523. $username_localhost="root";
  524. $password_localhost="";
  525. $json=array();
  526.  
  527. $conexion = mysqli_connect($hostname_localhost,$username_localhost,$password_localhost,$database_localhost);
  528. $consulta="select * from noticias order by id desc limit 20 offset 0";
  529. $resultado=mysqli_query($conexion,$consulta);
  530.  
  531. while($registro=mysqli_fetch_array($resultado)){
  532. $result["nombre"]=$registro['nombre'];
  533. $result["titulo"]=$registro['titulo'];
  534. $result["fecha"]=$registro['fecha'];
  535. $result["texto"]=$registro['texto'];
  536. $result["url1"]=$registro['url1'];
  537. $result["url2"]=$registro['url2'];
  538. $result["url3"]=$registro['url3'];
  539. $result["url4"]=$registro['url4'];
  540. $result["url5"]=$registro['url5'];
  541. $result["url6"]=$registro['url6'];
  542. $json['noticias'][]=$result;
  543. //echo $registro['id'].' - '.$registro['nombre'].'<br/>';
  544. }
  545. mysqli_close($conexion);
  546. echo json_encode($json);
  547. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement