如何更改片段中FormEditText或EditText的值?

时间:2015-03-16 16:05:14

标签: android android-fragments android-volley

我尝试在onCreate,onStart,甚至是onAttach()中进行,但是这些都不会改变。这些值保持与首次创建片段时的值相同。我就是这样做的

import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.andreabaccega.widget.FormEditText;
import com.thesis.rentalpropertymanagement.R;
import com.thesis.rentalpropertymanagement.framework.RPMApiRequests;
import com.thesis.rentalpropertymanagement.framework.VolleyController;
import com.thesis.rentalpropertymanagement.interfaces.OnApiSuccessCalback;
import com.thesis.rentalpropertymanagement.models.RentalProperty;

public class ProfileFragment
    extends Fragment
    implements OnApiSuccessCalback {

    public static String ARGS_PROFILE = "profile";

    private RentalProperty property;

    private Button updateBtn;

    private FormEditText nameTxt;
    private FormEditText contactNoTxt;
    private FormEditText emailTxt;
    private FormEditText usernameTxt;
    private FormEditText passwordTxt;
    private FormEditText priceConditionsTxt;
    private FormEditText bathsTxt;
    private FormEditText bedroomsTxt;
    private FormEditText totalRoomsTxt;
    private FormEditText fullyFurnishedTxt;
    private FormEditText yearBuiltTxt;

    private View fragment;

    @Override
    public View onCreateView(
        LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        fragment = inflater.inflate(R.layout.fragment_property_profile,
            container, false);
        property = new RentalProperty((String) getArguments().get(ARGS_PROFILE));
        setUpViews(fragment);
        Log.d("ERNEST", "ProfileFragment : " + property.getJsonData().toString());
        return fragment;
    }

    public void setUpViews(View fragment) {
        updateBtn = (Button) fragment.findViewById(R.id.updateBtn);
        updateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataAreValid()) {
                      VolleyController.instance.addToRequestQueue(RPMApiRequests
                        .updatePropertyInfo(getActivity(),
                            ProfileFragment.this, 1,
                            VolleyController.instance.getIpAddress(),
                            property));
                }

            }
        });
        nameTxt = (FormEditText) fragment.findViewById(R.id.name);
        nameTxt.setText(property.getLandlordName());

        contactNoTxt = (FormEditText) fragment.findViewById(R.id.contact_no);
        contactNoTxt.setText(property.getContactNo());

        emailTxt = (FormEditText) fragment.findViewById(R.id.email_ad);
        emailTxt.setText(property.getEmail());

        usernameTxt = (FormEditText) fragment.findViewById(R.id.username);
        usernameTxt.setText(property.getUsername());

        passwordTxt = (FormEditText) fragment.findViewById(R.id.password);
        passwordTxt.setText(property.getPassword());

        priceConditionsTxt = (FormEditText) fragment
            .findViewById(R.id.price_conditions);
        priceConditionsTxt.setText(property.getPriceConditions());

        bathsTxt = (FormEditText) fragment.findViewById(R.id.baths);
        bathsTxt.setText(String.valueOf(property.getBaths()));

        bedroomsTxt = (FormEditText) fragment.findViewById(R.id.bedrooms);
        bedroomsTxt.setText(String.valueOf(property.getBedrooms()));

        totalRoomsTxt = (FormEditText) fragment.findViewById(R.id.total_rooms);
        totalRoomsTxt.setText(String.valueOf(property.getTotalRooms()));

        fullyFurnishedTxt = (FormEditText) fragment
            .findViewById(R.id.fully_furnished);
        fullyFurnishedTxt.setText(getFullyFurnished());

        yearBuiltTxt = (FormEditText) fragment.findViewById(R.id.year_built);
        yearBuiltTxt.setText(property.getYearBuilt());
    }

    private String getFullyFurnished() {
        int fullyFurnished = property.isFullyFurnished();
        if (fullyFurnished == 1) {
            return "Y";
        } else {
            return "N";
        }
    }

    private int isFullyFurnished() {
        String fullyFurnished = fullyFurnishedTxt.getText().toString();
        if (fullyFurnished.toUpperCase().contains("Y")) {
            return 1;
        } else {
            return 0;
        }
    }

    public boolean dataAreValid() {
        if (nameTxt.testValidity() && contactNoTxt.testValidity()
            && emailTxt.testValidity() && usernameTxt.testValidity()
            && passwordTxt.testValidity()
            && priceConditionsTxt.testValidity() && bathsTxt.testValidity()
            && bedroomsTxt.testValidity() && totalRoomsTxt.testValidity()
            && fullyFurnishedTxt.testValidity()
            && yearBuiltTxt.testValidity()) {
            property = new RentalProperty(nameTxt.getText().toString(),
                contactNoTxt.getText().toString(), emailTxt.getText()
                .toString(), usernameTxt.getText().toString(),
                passwordTxt.getText().toString(), priceConditionsTxt
                .getText().toString(), Integer.parseInt(bathsTxt
                .getText().toString()),
                Integer.parseInt(bedroomsTxt.getText().toString()),
                Integer.parseInt(totalRoomsTxt.getText().toString()),
                isFullyFurnished(), yearBuiltTxt.getText().toString());
            return true;
        }
        return false;
    }

    @Override
    public void OnApiSuccess(JSONObject response, int requestCode) {
        // TODO Auto-generated method stub
    }
}

1 个答案:

答案 0 :(得分:0)

我在onActivityCreated

中设置了Textview的值
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


        inputSend = (TextView) getActivity().findViewById(R.id.inputSend);
        inputSend.setText("xxxxxxx");

}