【问题标题】:SetText on an editText in a pageadapter在 pageadapter 中的 editText 上的 SetText
【发布时间】:2012-04-02 21:30:04
【问题描述】:

这是我的问题:我有一个具有 3 个视图的页面适配器,左侧用于本地化用户并使用已本地化的地址的不同元素填充一些 editText 字段,这是方法:

    private boolean getAddressLocation(Location location) {

  if (location != null) {
   lat = location.getLatitude();
   longi = location.getLongitude();
   Geocoder gc = new Geocoder(NoteReminder.this, Locale.getDefault());
   try {
    List<Address> addresses = gc.getFromLocation(lat, longi, 1);
    if (addresses.size() > 0) {
         Address address = (Address) addresses.get(0);
         streetNumber = address.getAddressLine(0);
         locality = address.getLocality();
         postcode = address.getPostalCode();
         country = address.getCountryName();

         etCountry.setText(country, TextView.BufferType.EDITABLE);
         etPostcode.setText(postcode, TextView.BufferType.EDITABLE);
         etLocality.setText(locality, TextView.BufferType.EDITABLE);
         etAddressText.setText(streetNumber, TextView.BufferType.EDITABLE);



         return true;
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  return false;
}

问题是,如果本地化不够精确,但当我编辑这些字段并且我回到我的主要活动时,我希望用户能够编辑 editText(位置之一是我的页面适配器的左侧,这个在中间,最后一个是右侧)我有一个按钮可以将我的活动中的所有数据保存到我的 SQLite 数据库中......一切正常,但是当我修改 edditText 字段时从地址位置自动填充一些 setText 的地址存储在我的数据库中的值仍然是我的 editText 的自动填充字段...

    ...
etCountry = (EditText) findViewById(R.id.etCountry);
    etPostcode = (EditText) findViewById(R.id.etPostcode);
    etLocality  = (EditText) findViewById(R.id.etLocality);
    etAddressText = (EditText) findViewById(R.id.etAddressText);


         display = etAddressText.getText() + "," + etLocality.getText() + "," + etPostcode.getText()
           + "," + etCountry.getText();
...

我不明白?这是否意味着一旦 editText 填充了 .setText("...") 我们就不能再修改它了?

【问题讨论】:

    标签: android android-edittext android-viewpager settext


    【解决方案1】:

    验证:您似乎暗示您的字符串“display”正在显示旧数据。是这样吗?

    当您想从 EditTexts 中获取文本时,您是否在 PagerAdapter 的正确页面中查找那些 findViewById()s?

    您使用的是 FragmentPagerAdapter 吗?如果您的寻呼机页面是片段,您可能需要在它们之间传递信息,因为它们的生命周期发生变化。

    【讨论】:

      【解决方案2】:

      确实,我的字符串显示正在显示旧数据,

      Are you looking in the correct page of your PagerAdapter for those findViewById()s when     you want to get the text out of the EditTexts?
      

      ==> 我只是做一个简单的

      etCountry = (EditText) findViewById(R.id.etCountry);
      etPostcode = (EditText) findViewById(R.id.etPostcode);
      etLocality  = (EditText) findViewById(R.id.etLocality);
      etAddressText = (EditText) findViewById(R.id.etAddressText);
      

      来自我的 pageAdapter 的另一个页面

      在执行我的“getText”之前,我没有使用 fragmentPageAdapter 而是一个简单的 pageAdapter

      【讨论】:

      • 问题解决了!! ;-) 无论如何,谢谢你只需要使用一些 .getEditableText() 而不是 gettext
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多