2013-06-12 6 views
0

내가 찾은 것은 ViewBinder가 사용자 정의 커서 어댑터가 아니라는 것입니다. 그러나 문제는 내가 숨기거나 숨길 필요가 있는지 다시 결정하지 않는 것입니다. 그것의 단지 항상 그것을 숨기고있는 것뷰 파인더가있는 안드로이드 조각

편집 : 우리는 이미지를 보여주고, 필요할 때 그들을 숨기려고한다. 그러나 나는 스크롤 할 때 낙담한다.

public class HistoryFragment extends Fragment { 

    ListView listTimeline; 
    SimpleCursorAdapter adapter; 
    TextView txtCreatedAt, txtFertile, txtTemp, txtCervix; 
    ImageView imgIntercorse, imgEnergy, imgPregancy, imgMood, imgHeadache, imgPeriod; 
    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 }; 
    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}; 

    @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); 

     return view; 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     this.setupList(); 
     } 

    @Override 
public void onPause() { 
    super.onPause(); 
    } 

    private void setupList() { 
     txtCreatedAt = (TextView) getActivity().findViewById(R.id.txtCreatedAt); 
     txtFertile = (TextView) getActivity().findViewById(R.id.txtFertile); 
     txtTemp = (TextView) getActivity().findViewById(R.id.txtTemp); 
     txtCervix = (TextView) getActivity().findViewById(R.id.txtCervix); 
     imgIntercorse = (ImageView) getActivity().findViewById(R.id.imgIntercorse); 
     imgEnergy = (ImageView) getActivity().findViewById(R.id.imgEnergy); 
     imgPregancy = (ImageView) getActivity().findViewById(R.id.imgPregancy); 
     imgMood = (ImageView) getActivity().findViewById(R.id.imgMood); 
     imgHeadache = (ImageView) getActivity().findViewById(R.id.imgHeadache); 
     imgPeriod = (ImageView) getActivity().findViewById(R.id.imgPeriod); 


     // Get the data 
     Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_CHARTING, null, null , null, StatusData.KEY_CHARTING_DATE + " DESC"); // <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); 
     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"); 
      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"); 
       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. 
       String 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); 
        } 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_INTERCORSE)) { 
       // If the column is IS_STAR then we use custom view. 
       String 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); 
        } 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_MOOD)) { 
       // If the column is IS_STAR then we use custom view. 
       String 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); 
        } 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_HEADACHE)) { 
       // If the column is IS_STAR then we use custom view. 
       String 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); 
        } 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_TEST)) { 
       // If the column is IS_STAR then we use custom view. 
       String 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); 
        } 
       } 
       return true; 
      } 

      if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_ENERGY)) { 
       // If the column is IS_STAR then we use custom view. 
       String 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); 
        } 
       } 
       return true; 
      } 


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

    } 
} 

답변

1

일부 변수가 있습니다. 허용되는 값인 경우 if 조건에서 null 값을 확인하십시오. 그렇지 않다면이 데이터를 가져 오는 SQLite 테이블을 변경해야합니다. 비슷해 : 이러한 구조

CREATE TABLE some_table(_id INTEGER PRIMARY KEY AUTOINCREMENT, some_column TEXT NOT NULL)

, 삽입이 항목에 대한 값을 포함하지 않는다면, 이것은 실패한다.

0

가 나는 경우 NullPointerException이 레이아웃, android.R.layout.simple_list_item_1 (표준 안드로이드 목록 항목 레이아웃) 당신은을 사용하셨습니까 R.id.txtCreatedAt

의 ID로보기를 포함하지 않는 사실 때문이라고 생각 나만의 사용자 정의보기?

+0

나는 정직하게 나는 자습서를 따라 가고 싶다. 내가하고 싶은 것을 가르치기 위해 이곳 저곳에서 찾는다. 그러나 그들은 모두 온통 여기에있는 것처럼 보이거나 완전히 설명하지 않는다. 내가 가지고있는 것은 위에 게시 할 두 xml입니다. – jcaruso