如何在不触发其侦听器的情况下初始化某个视图的值

时间:2012-01-11 22:30:16

标签: java android android-listview simplecursoradapter

我有一个自定义SimpleCursorAdapter,可根据光标向ListView呈现自定义视图。在我的getView方法中,我对自定义视图进行了充气,​​并初始化其值。

在同一个适配器中,我为所有项目视图定义了侦听器。但每当我初始化时,值更改和此更改都会触发侦听器。这使我ListView滞后,因为它不是对数据库进行简单的“选择”,而是进行“选择”和“更新”。

这是我的适配器代码:

public class SmartFormCursorAdapter extends SimpleCursorAdapter implements
        CompoundButton.OnCheckedChangeListener, OnDateChangedListener,
        SeekBar.OnSeekBarChangeListener, RadioGroup.OnCheckedChangeListener {
    String sp_lib_name = null;
    Cursor c;
    Context context;
    TextView sbDisp = null;
    String idField = null;
    String idForm = null;

    public SmartFormCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.c = c;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = View.inflate(context, R.layout.smart_form_element, null);
        View row = convertView;
        c.moveToPosition(position);

        TextView fieldId = (TextView) convertView.findViewById(R.id.fieldId);
        TextView fieldType = (TextView) convertView.findViewById(R.id.fieldType);
        TextView fieldHint = (TextView) convertView.findViewById(R.id.fieldHint);
        TextView fieldName = (TextView) convertView.findViewById(R.id.fieldName);
        TextView formId = (TextView) convertView.findViewById(R.id.formId);

        String type = c.getString(c.getColumnIndexOrThrow("type"));
        idField = c.getString(c.getColumnIndexOrThrow("idField"));
        String hint = c.getString(c.getColumnIndexOrThrow("hint"));
        String name = c.getString(c.getColumnIndexOrThrow("fieldName"));
        idForm = c.getString(c.getColumnIndexOrThrow("idForm"));

        String currVal = H.getData(context, idField, idForm);
        // Log.w("SP currVal Field:" + idField, "Curr val: " + currVal);
        fieldId.setText(idField);
        fieldType.setText(type);
        fieldHint.setText(hint);
        fieldName.setText(name);
        formId.setText(idForm);
        // _________________________________
        // ________________________ TEXT
        // _________________________________

        if (type.equals("Text") || type.equals("Numeric")) {
            EditText et = (EditText) convertView.findViewById(R.id.valueText);
            if (type.equals("Text")) {
                et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
                et.setHeight(80);
            }
            if (type.equals("Numeric")) {
                et.setInputType(InputType.TYPE_CLASS_NUMBER);
                et.setHeight(40);
            }
            et.setTag(idField);
            et.setText(currVal);
            // et.setText("jjj");
            et.addTextChangedListener(new CustomTextWatcher(et) {

                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                public void afterTextChanged(Editable s) {
                    // Log.w("Text listener Field:" +
                    // this.getEditTextObj().getTag().toString(), "Result:" +
                    // s.toString());
                    H.saveData(context, this.getEditTextObj().getTag().toString(), s.toString(), idForm);
                }
            });
            et.setVisibility(View.VISIBLE);
        } else {
            EditText et = (EditText) convertView.findViewById(R.id.valueText);
            et.setVisibility(View.GONE);
        }
        // _________________________________
        // ________________________ DATE
        // _________________________________

        if (type.equals("Date")) {
            DatePicker dp = (DatePicker) convertView.findViewById(R.id.valueDate);
            dp.setTag(idField);
            String[] dateData = null;
            // Log.w("Date value Field:" + idField, "Result:" + currVal);

            if (!currVal.equals("err") && currVal.length() >= 8 && currVal.length() <= 10) {
                dateData = currVal.split("/");
                int month = Integer.valueOf(dateData[1]) - 1;
                dp.init(Integer.valueOf(dateData[2]), month, Integer.valueOf(dateData[0]), this);
            } else
                dp.init(2011, 1, 1, this);

            dp.setVisibility(View.VISIBLE);
        } else {
            DatePicker dp = (DatePicker) convertView.findViewById(R.id.valueDate);
            dp.setVisibility(View.GONE);
        }
        // _________________________________
        // ________________________SEEKBAR
        // _________________________________

        if (type.equals("Note")) {
            SeekBar sb = (SeekBar) convertView.findViewById(R.id.valueNote);
            sb.setVisibility(View.VISIBLE);
            sb.setTag(idField);
            if (!currVal.equals("err"))
                sb.setProgress(Integer.valueOf(currVal));
            sb.setOnSeekBarChangeListener(this);
            // Log.w("Curr value of SEEKBAR: " + idField, currVal);

        } else {
            SeekBar sb = (SeekBar) convertView.findViewById(R.id.valueNote);
            sb.setVisibility(View.GONE);
            sb.setTag(idField);
            ViewGroup r = (ViewGroup) sb.getParent();
            TextView sbDisp = (TextView) r.findViewById(R.id.valueNoteDisp);
            sbDisp.setVisibility(View.GONE);
        }
        // _________________________________
        // ________________________ Radio
        // _________________________________
        if (type.equals("Radio")) {
            RadioGroup rg = (RadioGroup) convertView.findViewById(R.id.valueRadio);
            rg.removeAllViewsInLayout();
            rg.setTag(idField);
            rg.setOnCheckedChangeListener(this);
            ArrayList<String> al = H.getFieldValues(context, idField);
            Iterator<String> itr = al.iterator();
            // Log.w("Curr value of RADIO: " + idField, currVal);
            while (itr.hasNext()) {
                String val = itr.next();

                String[] valData = val.split("##");
                // Log.w("VALUES FIELD: " + idField, val + "## 0: " + valData[0]
                // + " --1:" + valData[1]);
                RadioButton tempRB = new RadioButton(context);
                tempRB.setText(valData[1]);
                tempRB.setTag(valData[0]);
                // --init val
                if (currVal.equals(valData[0]))
                    tempRB.setChecked(true);
                else
                    tempRB.setChecked(false);
                // --init color
                tempRB.setTextColor(Color.BLUE);
                LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
                rg.addView(tempRB, 0, layoutParams);
            }
            rg.setVisibility(View.VISIBLE);

        } else {
            RadioGroup rg = (RadioGroup) convertView.findViewById(R.id.valueRadio);
            rg.setVisibility(View.GONE);
            rg.removeAllViewsInLayout();
        }
        // _________________________________
        // ________________________CHECKBOX
        // _________________________________
        if (type.equals("Booleen")) {
            CheckBox cb = (CheckBox) convertView.findViewById(R.id.valueBooleen);
            // Log.w("Curr value of CHECKBOXX: " + idField, currVal);
            cb.setVisibility(View.VISIBLE);
            cb.setTag(idField);
            cb.setOnCheckedChangeListener(this);
            if (currVal.equals("1"))
                cb.setChecked(true);
            else
                cb.setChecked(false);

        } else {
            CheckBox cb = (CheckBox) convertView.findViewById(R.id.valueBooleen);
            cb.setVisibility(View.INVISIBLE);
        }

        return (convertView);
    }

    // checkbox listeners
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // Log.w("Check box-is position", String.valueOf(isChecked) +
        // " - FieldId= " + buttonView.getTag());
        H.saveData(context, buttonView.getTag().toString(), isChecked ? "1" : "0", idForm);
    }

    // seekbar listeners
    public void onStartTrackingTouch(SeekBar seekBar) {
        ViewGroup r = (ViewGroup) seekBar.getParent();
        TextView sbDisp = (TextView) r.findViewById(R.id.valueNoteDisp);
        sbDisp.setVisibility(View.VISIBLE);

    }

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        H.saveData(context, seekBar.getTag().toString(), String.valueOf(progress), idForm);

    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        ViewGroup r = (ViewGroup) seekBar.getParent();
        TextView sbDisp = (TextView) r.findViewById(R.id.valueNoteDisp);
        sbDisp.setVisibility(View.VISIBLE);
        sbDisp.setText(String.valueOf(seekBar.getProgress()));
        notifyDataSetChanged();
    }

    // radio listeners
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton rb = (RadioButton) group.findViewById(checkedId);
        if (rb != null) {
            H.saveData(context, group.getTag().toString(), rb.getTag().toString(), idForm);
            notifyDataSetChanged();
        }

    }

    // date listeners
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        String dateVal = String.valueOf(dayOfMonth) + "/" + String.valueOf(monthOfYear + 1) + "/" + String.valueOf(year);
        H.saveData(context, view.getTag().toString(), dateVal, idForm);
        // Log.w("onDateChanged", "- " + view.getTag().toString());

    }

}

这是我的项目自定义:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:background="@drawable/lv_bg_smart_form" android:minHeight="60px"
    android:layout_height="wrap_content">

    <TextView android:text="TextView" android:id="@+id/fieldName"
        android:textColor="#000000" android:textSize="18dip"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginLeft="10px" android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"></TextView>

    <!-- ________________________________________________________ -->

    <EditText android:layout_width="fill_parent" android:id="@+id/fieldId"
        android:layout_height="0dip" android:visibility="invisible" />
    <EditText android:layout_width="fill_parent" android:id="@+id/formId"
        android:layout_height="0dip" android:visibility="invisible" />
    <EditText android:layout_width="fill_parent" android:id="@+id/fieldHint"
        android:layout_height="0dip" android:visibility="invisible" />
    <EditText android:layout_width="fill_parent" android:id="@+id/fieldType"
        android:layout_height="0dip" android:visibility="invisible" />
    <!-- //Text, Date, , Numeric |||| Note |||| Radio |||| Booleen -->
    <!-- =========================================================== -->
    <!-- =========================================================== -->
    <!-- =========================================================== -->
    <!-- =========================================================== -->
    <CheckBox android:layout_height="wrap_content" android:id="@+id/valueBooleen"
        android:layout_width="wrap_content" android:visibility="invisible"
        android:layout_marginRight="20px" android:text=""
        android:layout_below="@+id/fieldName" android:layout_alignParentRight="true"></CheckBox>

    <RadioGroup android:layout_height="wrap_content" android:id="@+id/valueRadio"
        android:layout_marginRight="20px" android:visibility="invisible"
        android:layout_width="wrap_content" android:layout_below="@+id/fieldName"
        android:layout_alignParentRight="true">
    </RadioGroup>


    <SeekBar android:id="@+id/valueNote" android:max="100"

        android:progressDrawable="@drawable/seekbar_progress" android:thumb="@drawable/seek_thumb_normal"
        android:layout_height="wrap_content" android:layout_marginRight="20px"
        android:layout_below="@+id/fieldName" android:layout_alignParentRight="true"
        android:layout_width="400px">

    </SeekBar>
    <TextView android:text="" android:id="@+id/valueNoteDisp"
        android:textColor="#555599" android:textSize="12dip"
        android:visibility="invisible" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_marginLeft="10px"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"></TextView>


    <EditText android:inputType="textMultiLine"
        android:layout_width="400px" android:id="@+id/valueText"
        android:layout_marginRight="20px" android:layout_below="@+id/fieldName"
        android:layout_alignParentRight="true" android:layout_height="90px"
        android:visibility="invisible">
    </EditText>

    <DatePicker android:id="@+id/valueDate" android:visibility="invisible"
        android:layout_marginRight="5px" android:layout_height="wrap_content"
        android:layout_alignParentRight="true" android:layout_alignParentTop="true"
        android:layout_width="wrap_content"></DatePicker>


</RelativeLayout>

请帮忙。

0 个答案:

没有答案