Advertisement
andyshon

refreshScreen

Jan 19th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.53 KB | None | 0 0
  1. public void refreshScreen_SimpleWay() {
  2. Call<List<Section>> call = restClient.getService().getSectionsByCourseId(_Course_Id);
  3.                 call.enqueue(new Callback<List<Section>>() {
  4.                                  @Override
  5.                                  public void onResponse(Call<List<Section>> call, Response<List<Section>> response) {
  6.                                      final ListView lv = (ListView) findViewById(R.id.listView);
  7.  
  8.  
  9.                                      final ArrayList<HashMap<String, String>> sectionList = new ArrayList<HashMap<String, String>>();
  10.  
  11.                                      for (int i = 0; i < response.body().size(); i++) {
  12.                                          HashMap<String, String> section = new HashMap<String, String>();
  13.  
  14.                                          section.put("id", String.valueOf(response.body().get(i).getId()));
  15.                                          section.put("name", String.valueOf(response.body().get(i).getName()));
  16.  
  17.                                          sectionList.add(section);
  18.                                      }
  19.  
  20.                                      final ListAdapter adapter = new SimpleAdapter(CourseManagerActivity.this, sectionList, R.layout.view_user_entry,
  21.                                              new String[]{"id", "name"}, new int[]{R.id.user_Id, R.id.user_name});
  22.  
  23.                                      lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  24.                                          @Override
  25.                                          public boolean onItemLongClick(AdapterView<?> parent, final View view, int position, long id) {
  26.  
  27.                                              final TextView tvName = view.findViewById(R.id.user_name);
  28.                                              TextView tvId = view.findViewById(R.id.user_Id);
  29.                                              _Course_Id = tvId.getText().toString();                                          
  30.  
  31.                                              AlertDialog.Builder ad;
  32.                                              String title = "Вы действительно хотите удалить '" + tvName.getText() + "'?";
  33.                                              String message = "Удалить?";
  34.                                              String button1String = "Да";
  35.                                              String button2String = "Нет";
  36.  
  37.                                              ad = new AlertDialog.Builder(CourseManagerActivity.this);
  38.                                              ad.setMessage(title); // сообщение
  39.                                              ad.setPositiveButton(button1String, new DialogInterface.OnClickListener() {
  40.                                                  public void onClick(DialogInterface dialog, int arg1) {
  41.  
  42.                                                      System.out.println("_Course_Id:::: " + _Course_Id);
  43.                                                      Call<Void> call = restClient.getService().deleteSectionAndChildrenById(_Course_Id);
  44.                                                      call.enqueue(new Callback<Void>() {
  45.                                                          @Override
  46.                                                          public void onResponse(@NonNull Call<Void> call, @NonNull Response<Void> response) {
  47.                                                              if (response.code() == 200) {
  48.                                                                  Toast.makeText(CourseManagerActivity.this, tvName.getText() + " успешно удален!",
  49.                                                                          Toast.LENGTH_LONG).show();
  50.  
  51.                                                                  refreshScreen_SimpleWay();
  52. }
  53.                                                              else {
  54.                                                                  Toast.makeText(CourseManagerActivity.this, "Error: Not Deleted" + response.errorBody(), Toast.LENGTH_LONG).show();
  55.                                                              }
  56.                                                          }
  57.  
  58.                                                          @Override
  59.                                                          public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
  60.                                                              Toast.makeText(CourseManagerActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
  61.                                                          }
  62.                                                      });
  63.  
  64.  
  65.                                                  }
  66.                                              });
  67.                                              ad.setNegativeButton(button2String, new DialogInterface.OnClickListener() {
  68.                                                  public void onClick(DialogInterface dialog, int arg1) {
  69.                                                      Toast.makeText(CourseManagerActivity.this, "Отмена", Toast.LENGTH_LONG).show();
  70.                                                  }
  71.                                              });
  72.                                              ad.setCancelable(true);
  73.                                              ad.setOnCancelListener(new DialogInterface.OnCancelListener() {
  74.                                                  public void onCancel(DialogInterface dialog) {
  75.                                                      Toast.makeText(CourseManagerActivity.this, "Отмена", Toast.LENGTH_LONG).show();
  76.                                                  }
  77.                                              });
  78.                                              ad.show();
  79.  
  80.                                              return true;
  81.                                          }
  82.                                      });
  83.  
  84.                                      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  85.                                          @Override
  86.                                          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  87.                                              tvSection_Id = (TextView) view.findViewById(R.id.user_Id);
  88.                                              String sectionId = tvSection_Id.getText().toString();
  89.  
  90.                                              section_name = (TextView) view.findViewById(R.id.user_name);
  91.                                              String sectionName = section_name.getText().toString();
  92.  
  93.                                              Intent objIndent = new Intent(CourseManagerActivity.this, SectionManagerActivity.class);
  94.                                              objIndent.putExtra("section_Id", sectionId);
  95.                                              objIndent.putExtra("section_title", sectionName);
  96.                                              startActivity(objIndent);
  97.                                          }
  98.                                      });
  99.  
  100.  
  101.                                      lv.setAdapter(adapter);
  102.  
  103.                                  }
  104.  
  105.                                  @Override
  106.                                  public void onFailure(@NonNull Call<List<Section>> call, @NonNull Throwable t) {
  107.                                      Toast.makeText(CourseManagerActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
  108.  
  109.                                  }
  110.                              }
  111.                 );
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement