0

我发现我需要一个 ViewBinder 而不是自定义光标 addapter 但是我遇到的问题是,在每一行上它都不会重新确定是否需要隐藏它。它只是总是把它隐藏起来

编辑:我们已经让它显示图像并在需要时隐藏它们,但当我滚动时它们令人沮丧。

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

    }
}
4

2 回答 2

1

你有一些空变量。如果这是一个可接受的值,那么只需检查您的if条件中的空值。如果不是,那么您需要更改从中提取此数据的 SQLite 表。就像是:

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

使用这种结构,如果插入不包含该列的值,那么它将失败。

于 2013-06-13T17:09:45.263 回答
0

我认为 NullPointerException 是由于您的布局android.R.layout.simple_list_item_1(标准的 Android 列表项布局)不包含 id 的视图R.id.txtCreatedAt

您的意思是使用您自己的自定义视图吗?

于 2013-06-13T02:08:02.967 回答