在Fragment中的onActivityResult之后,Android Textview文本无法更改

时间:2015-08-02 18:54:01

标签: android android-fragments textview

我正在尝试通过片段中的onActivityResult上的setText()更改TextView。 但是没有效果。 这是我的代码

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (requestCode == 111) {
        if (resultCode == mainActivity.RESULT_OK) {
            String result = data.getStringExtra("districtName");
            Log.w(AppConstants.TAG, " onActivityResult Buy " + requestCode
                    + " " + resultCode + " " + result);
            locationTvBuy.setText(result);
        }
        if (resultCode == mainActivity.RESULT_CANCELED) {
            // Write your code if there's no result
        }
    }
}

这是我的完整片段类代码`public class BuyFragment extends Fragment实现OnClickListener {

private Context context;

private TableRow loacationSelectBuy;
private TextView locationTvBuy;

private MainActivity mainActivity;

public BuyFragment(MainActivity home) {
    // TODO Auto-generated constructor stub
    this.mainActivity = home;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_buy, container,
            false);

    context = mainActivity;

    initUI(rootView);

    return rootView;
}

private void initUI(View view) {
    locationTvBuy = (TextView) view.findViewById(R.id.locationTvBuy);

    loacationSelectBuy = (TableRow) view
            .findViewById(R.id.loacationSelectBuy);
    loacationSelectBuy.setOnClickListener(this);

    Tools.rangedSeekbar(view, R.id.minValueTk, R.id.maxValueTk,
            R.id.layoutTkSeekBar, 0, 50000000, context);
    Tools.rangedSeekbar(view, R.id.minValueSf, R.id.maxValueSf,
            R.id.layoutSfSeekbar, 0, 20000, context);

}

String result = "Select Location";

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (requestCode == 111) {
        if (resultCode == mainActivity.RESULT_OK) {
            result = data.getStringExtra("districtName");
            Log.w(AppConstants.TAG, " onActivityResult Buy " + requestCode
                    + " " + resultCode + " " + result);
            // locationTvBuy.setText(result);
        }
        if (resultCode == mainActivity.RESULT_CANCELED) {
            // Write your code if there's no result
        }
    }
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.w(AppConstants.TAG, " Resume "+result);
    locationTvBuy.setText(result);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.loacationSelectBuy:
        Intent districtIntent = new Intent(getActivity(),
                DistrictActivity.class);
        mainActivity.startActivityForResult(districtIntent, 111);
        break;
    default:
        break;
    }
}

}` 在log中我找到了结果,但结果没有设置为TextView 需要一些帮助

1 个答案:

答案 0 :(得分:0)

您必须在onActivityResult中保留该值,但在onStart方法中将其设置在TextView中。