2013-07-25 1 views
0

J'ai donc une liste de 7 jours d'informations à afficher. Lorsque la liste est remplie, le @+id/image s'affiche correctement (pour autant que je puisse voir) et lorsque je fais défiler vers le bas et sauvegarder, l'image est modifiée. Dans le cas où l'image est peuplée avec une autre image, cela fonctionne bien, dans le cas où une image est remplie avec une couleur est en désordre.Listview et CustomViewBinder

Je sais qu'il y a probablement de la place à l'amélioration ici, donc si vous avez des suggestions, veuillez les expliquer en me montrant comment, merci d'avance!

public class HistoryFragment extends Fragment{ 

    ListView listTimeline; 
    SimpleCursorAdapter adapter; 
    String is_period = "", is_test = "", is_headache = "", is_energy = "", is_mood = "", is_stamp = "", is_intercorse = "", is_fertile= "", is_cervix="", is_temp = ""; 
    IntentFilter filter; 

    static final String[] FROM = { StatusData.KEY_CHARTING_DATE, StatusData.KEY_CHARTING_NOTES, StatusData.KEY_CHARTING_FERTILE, 
            StatusData.KEY_CHARTING_TEMPERATURE, StatusData.KEY_CHARTING_PERIOD, StatusData.KEY_CHARTING_INTERCORSE, 
            StatusData.KEY_CHARTING_MOOD, StatusData.KEY_CHARTING_HEADACHE, StatusData.KEY_CHARTING_TEST, 
            StatusData.KEY_CHARTING_ENERGY, StatusData.KEY_CHARTING_STAMPS, StatusData.KEY_CHARTING_CERVIX }; 
    static final int[] TO = { R.id.txtCreatedAt, R.id.txtNote, R.id.txtFertile, 
           R.id.txtTemp, R.id.imgPeriod, R.id.imgIntercorse, 
           R.id.imgMood, R.id.imgHeadache, R.id.imgPregancy, 
           R.id.imgEnergy, R.id.image, R.id.txtCervix}; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_history, container, false); 
     listTimeline = (ListView) view.findViewById(R.id.listTimeline); 

     setupList(); 

     return view; 
    } 

    private void setupList() { 
     // Get the data 
     Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_CHARTING, null, null , null, StatusData.KEY_CHARTING_DATE + " DESC" + " LIMIT 7"); // <3> 

     // create the adapter using the cursor pointing to the desired data 
     //as well as the layout information 
     adapter = new SimpleCursorAdapter(getActivity(), R.layout.history_row, c, FROM, TO, 0){ 
      @Override 
      public boolean isEnabled(int position) { 
       return false; 
      } 
     }; 
     adapter.setViewBinder(new CustomViewBinder()); 

     // Assign adapter to ListView 
     listTimeline.setAdapter(adapter); 
    } 

    private class CustomViewBinder implements ViewBinder { 

     private Date parseDate(String date) { 
      SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd", Locale.US); 
      Date dateObj = new Date(); 

      try { 
       dateObj = curFormater.parse(date); 
      } catch (java.text.ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return dateObj; 
     } 

     @Override 
     public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_DATE)) { 
       SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.US); 
       String date = cursor.getString(columnIndex); 
       Date dateObj = parseDate(date); 

       String formatedDate = format.format(dateObj); 

       TextView tv = (TextView) view; 
       tv.setText(formatedDate); 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_PERIOD)) { 
       // If the column is IS_STAR then we use custom view. 
       is_period = cursor.getString(columnIndex); 
       if (is_period != null) { 
        if (is_period.equalsIgnoreCase("no")){ 
         // set the visibility of the view to GONE 
         view.setVisibility(View.INVISIBLE); 
        } else { 
         view.getLayoutParams().height = 40; 
         view.getLayoutParams().width = 40; 
         view.setVisibility(View.VISIBLE); 
        } 
       } else { 
        view.setVisibility(View.INVISIBLE); 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_INTERCORSE)) { 
       // If the column is IS_STAR then we use custom view. 
       is_intercorse = cursor.getString(columnIndex); 
       if (is_intercorse != null) { 
        if (is_intercorse.equalsIgnoreCase("no")) { 
         // set the visibility of the view to GONE 
         view.setVisibility(View.INVISIBLE); 
        } else { 
         view.getLayoutParams().height = 40; 
         view.getLayoutParams().width = 40; 
         view.setVisibility(View.VISIBLE); 
        } 
       } else { 
        view.setVisibility(View.INVISIBLE); 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_ENERGY)) { 
       // If the column is IS_STAR then we use custom view. 
       is_energy = cursor.getString(columnIndex); 
       if (is_energy != null) { 
        if (is_energy.equalsIgnoreCase("no")) { 
         // set the visibility of the view to GONE 
         view.setVisibility(View.INVISIBLE); 
        } else { 
         Drawable is_energy_draw = getResources().getDrawable(getResources().getIdentifier("drawable/" + is_energy, null, getActivity().getPackageName())); 
         view.setBackground(is_energy_draw); 
         view.getLayoutParams().height = 40; 
         view.getLayoutParams().width = 40; 
         view.setVisibility(View.VISIBLE); 
        } 
       } else { 
        view.setVisibility(View.INVISIBLE); 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_HEADACHE)) { 
       // If the column is IS_STAR then we use custom view. 
       is_headache = cursor.getString(columnIndex); 
       if (is_headache != null) { 
        if (is_headache.equalsIgnoreCase("no")) { 
         // set the visibility of the view to GONE 
         view.setVisibility(View.INVISIBLE); 
        } else { 
         view.getLayoutParams().height = 40; 
         view.getLayoutParams().width = 40; 
         view.setVisibility(View.VISIBLE); 
        } 
       } else { 
        view.setVisibility(View.INVISIBLE); 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_TEST)) { 
       // If the column is IS_STAR then we use custom view. 
       is_test = cursor.getString(columnIndex); 
       if (is_test != null) { 
        if (is_test.equalsIgnoreCase("no")) { 
        // set the visibility of the view to GONE 
        view.setVisibility(View.INVISIBLE); 
        } else { 
         view.getLayoutParams().height = 40; 
         view.getLayoutParams().width = 40; 
         view.setVisibility(View.VISIBLE); 
        } 
       } else { 
        view.setVisibility(View.INVISIBLE); 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_MOOD)) { 
       // If the column is IS_STAR then we use custom view. 
       is_mood = cursor.getString(columnIndex); 

       if (is_mood != null) { 
        if (is_mood.equalsIgnoreCase("no")) { 
         // set the visibility of the view to GONE 
         view.setVisibility(View.INVISIBLE); 
        } else { 
         Drawable is_mood_draw = getResources().getDrawable(getResources().getIdentifier("drawable/" + is_mood, null, getActivity().getPackageName())); 
         view.setBackground(is_mood_draw); 
         view.getLayoutParams().height = 40; 
         view.getLayoutParams().width = 40; 
         view.setVisibility(View.VISIBLE); 
        } 
       } else { 
        view.setVisibility(View.INVISIBLE); 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_FERTILE)) { 
       // If the column is IS_STAR then we use custom view. 
       is_fertile = cursor.getString(columnIndex); 
       if (is_fertile != null) { 
        ((TextView) view).setText(is_fertile); 
       } else { 
        ((TextView) view).setText("No"); 
        view.setVisibility(View.INVISIBLE); 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_CERVIX)) { 
       // If the column is IS_STAR then we use custom view. 
       is_cervix = cursor.getString(columnIndex); 

       if (is_cervix != null) { 
        ((TextView) view).setText(is_cervix); 
       } else { 
        ((TextView) view).setText("No"); 
        view.setVisibility(View.INVISIBLE); 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_TEMPERATURE)) { 
       // If the column is IS_STAR then we use custom view. 
       is_temp = cursor.getString(columnIndex); 

       if (is_temp != null) { 
        ((TextView) view).setText(is_temp); 
       } else { 
        ((TextView) view).setText("No"); 
        view.setVisibility(View.INVISIBLE); 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_STAMPS)) { 
       is_stamp = cursor.getString(columnIndex); 
       if (is_stamp != null) { 
        if (is_stamp.equalsIgnoreCase("no")) { 
         // set the visibility of the view to GONE 
         if (!is_fertile.equalsIgnoreCase("no") && !is_fertile.equalsIgnoreCase("")) { 
          view.setBackgroundColor(Color.YELLOW); 
         } else if(!is_cervix.equalsIgnoreCase("no") && !is_cervix.equalsIgnoreCase("0") && !is_cervix.equalsIgnoreCase("")) { 
          view.setBackgroundColor(Color.BLACK); 
         } else if(!is_temp.equalsIgnoreCase("no") && !is_temp.equalsIgnoreCase("0") && !is_temp.equalsIgnoreCase("")) { 
          view.setBackgroundColor(Color.WHITE); 
         } else if(!is_period.equalsIgnoreCase("no") && !is_period.equalsIgnoreCase("")) { 
          view.setBackgroundColor(Color.RED); 
         } 

         view.setVisibility(View.VISIBLE); 
        } else { 
         Drawable is_stamp_draw = getResources().getDrawable(getResources().getIdentifier("drawable/" + is_stamp, null, getActivity().getPackageName())); 
         view.setBackground(is_stamp_draw); 
         view.getLayoutParams().height = 211; 
         view.getLayoutParams().width = 120; 
         view.setVisibility(View.VISIBLE); 
        } 
       } else { 
        view.setVisibility(View.VISIBLE); 
       } 
      } 

      // For others, we simply return false so that the default binding 
      // happens. 
      return false; 
     } 
    } 
} 

liste xml:

<!-- <1> --> 
    <ListView 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:id="@+id/listTimeline" /> 

</LinearLayout> 

ligne xml:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="120px" 
     android:layout_height="211px" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" /> 

    <TextView 
     android:id="@+id/lblFertile" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/txtCreatedAt" 
     android:layout_below="@+id/txtCreatedAt" 
     android:text="@string/lblFertile" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/txtCervix" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/lblCervix" 
     android:layout_alignBottom="@+id/lblCervix" 
     android:layout_toRightOf="@+id/lblCervix" 
     android:paddingLeft="5sp" 
     android:text="@string/NA" /> 

    <TextView 
     android:id="@+id/txtFertile" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/lblFertile" 
     android:layout_alignBottom="@+id/lblFertile" 
     android:layout_toRightOf="@+id/lblFertile" 
     android:paddingLeft="5sp" 
     android:text="@string/NA" /> 

    <TextView 
     android:id="@+id/txtCreatedAt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="24dp" 
     android:layout_toRightOf="@+id/image" 
     android:gravity="left" 

     android:text="@string/defDate" /> 

    <TextView 
     android:id="@+id/lblCervix" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/txtFertile" 
     android:layout_marginLeft="24dp" 
     android:layout_toRightOf="@+id/txtCreatedAt" 
     android:text="@string/lblCervix" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/lblNotes" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/lblTemp" 
     android:layout_below="@+id/lblTemp" 
     android:text="@string/lblNotes" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/txtNote" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/lblNotes" 
     android:layout_below="@+id/lblNotes" 
     android:text="@string/NA" /> 

    <TextView 
     android:id="@+id/lblTemp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/lblFertile" 
     android:layout_below="@+id/lblFertile" 
     android:text="@string/lblTemp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/txtTemp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/lblNotes" 
     android:layout_alignLeft="@+id/txtFertile" 
     android:paddingLeft="5sp" 
     android:text="@string/NA" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/lblFertile" 
     android:layout_alignBottom="@+id/image" 
     android:layout_toRightOf="@+id/image" > 

     <ImageView 
      android:id="@+id/imgIntercorse" 
      android:layout_width="20dp" 
      android:layout_height="20dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:src="@drawable/intercorse" /> 

     <ImageView 
      android:id="@+id/imgPregancy" 
      android:layout_width="20dp" 
      android:layout_height="20dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:src="@drawable/pregnancy" /> 

     <ImageView 
      android:id="@+id/imgPeriod" 
      android:layout_width="20dp" 
      android:layout_height="20dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:src="@drawable/period" /> 

     <ImageView 
      android:id="@+id/imgHeadache" 
      android:layout_width="20dp" 
      android:layout_height="20dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:src="@drawable/headache" /> 

     <ImageView 
      android:id="@+id/imgMood" 
      android:layout_width="20dp" 
      android:layout_height="20dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="10dp" /> 

     <ImageView 
      android:id="@+id/imgEnergy" 
      android:layout_width="20dp" 
      android:layout_height="20dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/energy_1" /> 

    </LinearLayout> 

    </RelativeLayout> 

</LinearLayout> 
+0

* dans le cas où une image est remplie avec une couleur est en désordre. * - que signifie exactement le désordre? – Luksprog

+0

Leurs couleurs changent, disons par exemple que ma première couleur est jaune, et la deuxième est noire, et la troisième est une image. Lorsque je défile vers le bas et que je sauvegarde, la première image est jaune et la seconde est rouge, mais l'image reste la même. – jcaruso

+0

Double-vérifier les clauses if là je ne sais pas ce que ces champs signifient donc je ne peux pas vous aider. Vous devriez également avoir une autre clause pour vous assurer absolument que vous couvrez tous les cas. – Luksprog

Répondre

0

I fini par fixation par arrêt en utilisant la couleur de setBackground et en utilisant une image à la place.