Guest User

Untitled

a guest
Jun 8th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. package com.attilapalf.exceptional.ui.main;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Color;
  5. import android.os.Bundle;
  6. import android.support.annotation.Nullable;
  7. import android.support.v4.app.ListFragment;
  8.  
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.TextView;
  14.  
  15. import com.attilapalf.exceptional.R;
  16. import com.attilapalf.exceptional.exception.*;
  17. import com.attilapalf.exceptional.exception.Exception;
  18.  
  19. import java.util.Arrays;
  20.  
  21. /**
  22.  */
  23. public class ExceptionsFragment extends ListFragment {
  24.  
  25.     private ArrayAdapter<Exception> adapter;
  26.  
  27.     @Override
  28.     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  29.         super.onActivityCreated(savedInstanceState);
  30.  
  31.         ExceptionPreferences exceptionPreferences = new ExceptionPreferences(getActivity().getApplicationContext());
  32.         Exception[] values = exceptionPreferences.getExceptionArray();
  33.         Arrays.sort(values, new Exception.NameComparator());
  34.         adapter = new MyAdapter(getActivity().getApplicationContext(), values, exceptionPreferences);
  35.         adapter.notifyDataSetChanged();
  36.         setListAdapter(adapter);
  37.     }
  38.  
  39.  
  40.     @Nullable
  41.     @Override
  42.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  43.         // Inflate the layout for this fragment
  44.         return inflater.inflate(R.layout.fragment_exceptions, null);
  45.     }
  46.  
  47.  
  48.     public void notifyDatasetChanged() {
  49.         adapter.notifyDataSetChanged();
  50.     }
  51.  
  52.  
  53.     private static class MyAdapter extends ArrayAdapter<com.attilapalf.exceptional.exception.Exception> {
  54.         private Context context;
  55.         private Exception[] values;
  56.         private ExceptionPreferences exceptionPreferences;
  57.  
  58.         public MyAdapter(Context context, Exception[] values, ExceptionPreferences preferences) {
  59.             super(context, R.layout.exc_row_layout, values);
  60.             exceptionPreferences = preferences;
  61.             this.context = context;
  62.             this.values = values;
  63.         }
  64.  
  65.  
  66.         @Override
  67.         public void notifyDataSetChanged() {
  68.             values = exceptionPreferences.getExceptionArray();
  69.             Arrays.sort(values, new Exception.NameComparator());
  70.             super.notifyDataSetChanged();
  71.         }
  72.  
  73.         @Override
  74.         public View getView(int position, View convertView, ViewGroup parent) {
  75.             RowViewHolder viewHolder;
  76.  
  77.             LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  78.             //View rowView = inflater.inflate(R.layout.exc_row_layout, parent, false);
  79.             if (convertView == null) {
  80.                 convertView = inflater.inflate(R.layout.exc_row_layout, parent, false);
  81.                 viewHolder = new RowViewHolder(convertView);
  82.                 convertView.setTag(viewHolder);
  83.  
  84.             } else {
  85.                 viewHolder = (RowViewHolder)convertView.getTag();
  86.             }
  87.  
  88.             viewHolder.bindRow(values[position]);
  89.  
  90.             return convertView;
  91.         }
  92.  
  93.  
  94.         private static class RowViewHolder {
  95.             private TextView nameView;
  96.             private TextView descView;
  97.  
  98.             public RowViewHolder(View rowView) {
  99.                 nameView = (TextView) rowView.findViewById(R.id.excName);
  100.                 descView = (TextView) rowView.findViewById(R.id.excDesc);
  101.  
  102.                 nameView.setTextSize(20);
  103.                 descView.setTextSize(15);
  104.                 nameView.setTextColor(Color.BLACK);
  105.                 descView.setTextColor(Color.BLACK);
  106.             }
  107.  
  108.             public void bindRow(Exception model) {
  109.                 nameView.setText(model.getName());
  110.                 descView.setText(model.getDescription());
  111.             }
  112.         }
  113.     }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment