Pergunta

We've gotten it to show the images and hide them when needed but they are Disappearing when i scroll down or up. Anyone every seen this or know its cause? Not sure whats going on but when i scroll its as if it is redetermine if it needs to be displayed and its going with no even though for that date it is yes.

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

    }
}

XML 1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" 
  android:layout_height="fill_parent"
  android:layout_width="fill_parent">

  <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center"
        android:layout_margin="10dp" 
        android:text="@string/title_history"
        android:textSize="30sp" />

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


</LinearLayout>

XML2:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content" 
     android:orientation="vertical">

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <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/lblTemp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+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_alignBaseline="@+id/lblTemp"
        android:layout_alignBottom="@+id/lblTemp"
        android:layout_alignLeft="@+id/txtFertile"
        android:paddingLeft="5sp"
        android:text="@string/NA" />

    <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:paddingLeft="20sp"
        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" />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imgIntercorse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pregnancy_history"
            android:layout_marginLeft="70dp"
            android:layout_marginTop="10dp" />

        <ImageView
            android:id="@+id/imgPregancy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pregnancy_history" 
            android:layout_marginLeft="14dp"
            android:layout_marginTop="10dp" />

        <ImageView
            android:id="@+id/imgPeriod"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/period_history" 
            android:layout_marginLeft="14dp"
            android:layout_marginTop="10dp" />

        <ImageView
            android:id="@+id/imgHeadache"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pregnancy_history"
            android:layout_marginLeft="14dp"
            android:layout_marginTop="10dp"  />

        <ImageView
            android:id="@+id/imgMood"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pregnancy_history" 
            android:layout_marginLeft="14dp"
            android:layout_marginTop="10dp" />
        <ImageView
            android:id="@+id/imgEnergy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pregnancy_history" 
            android:layout_marginLeft="14dp"
            android:layout_marginTop="10dp" />

    </LinearLayout>
    <TextView
            android:id="@+id/txtNote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/NA" 
            android:layout_marginTop="10dp" />
</LinearLayout>
Foi útil?

Solução

Even in a ViewBinder you need to always provide a revert option for the view changes to handle a possible recycled row. I don't know which are your images so here is an example:

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);
                } else {
                    // bring the view to its default state which I'm assuming is being visible
                    view.setVisibility(View.VISIBLE);
                }
            }
            return true;
        }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top