2013-07-25 4 views
0

그래서 7 일 동안 표시 할 정보 목록이 있습니다. 목록이 채워지면 @+id/image이 제대로 표시됩니다 (내가 볼 수있는 한). 그리고 아래로 스크롤하여 백업하면 이미지가 변경됩니다. 이미지가 다른 이미지로 채워져있는 경우 이미지가 잘 채워진 경우 색상이 엉망입니다.Listview 및 CustomViewBinder

여기에 개선의 여지가 있음을 알고 있습니다. 제안 사항이 있으면 설명해주십시오. 미리 감사드립니다.

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; 
     } 
    } 
} 

목록 XML :

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

</LinearLayout> 

행 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

* 색상이있는 이미지가있는 경우 * 정확히 무슨 뜻입니까? – Luksprog

+0

색상이 변합니다. 예를 들어 첫 번째 색상은 노란색이고 두 번째 색상은 검은 색이며 세 번째 색상은 이미지입니다. 아래쪽으로 스크롤하여 뒤로 이동하면 첫 번째 이미지는 노란색이고 두 번째 이미지는 빨간색이지만 이미지는 그대로 유지됩니다. – jcaruso

+0

if 절을 두 번 확인하면 해당 필드의 의미를 알 수 없으므로 도와 줄 수 없습니다. 모든 케이스를 확실히 다루기 위해서는 else 절도 있어야합니다. – Luksprog

답변

0

내가 setBackground의 색상을 사용하는 대신 이미지를 사용 중지하여 고정 끝났다.