0

所以我有一个 7 天的信息列表来显示。当列表被填充时,@+id/image显示正确(据我所知),当我向下滚动和备份时,图像发生了变化。在图像填充另一个图像的情况下,它可以正常工作,在图像填充颜色的情况下,它会变得混乱。

我知道这里可能有一些改进的空间,所以如果你有建议,请解释它们并告诉我如何,提前谢谢!

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>
4

1 回答 1

0

我最终通过停止使用 setBackground 颜色并改用图像来修复它。

于 2013-07-31T18:22:21.543 回答