【问题标题】:textView not refreshing in fragment with updated text (Android Development)textView 没有在片段中刷新并更新文本(Android 开发)
【发布时间】:2014-03-23 04:31:02
【问题描述】:

在我的 Android 项目中,我有一个扩展 FragmentActivity 的类,以显示 2 个选项卡(它们是片段)。我正在尝试将数据从一个 Activity 中的 JSON 对象传递到其中一个选项卡上的 textView。我按照本教程创建标签:http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/ JSON 对象已成功传递给相应的 FragmentActivity,并且已成功设置 textView。但是,它不会显示在选项卡上。

由于我在另一个包中设置了选项卡,因此我使用了 LayoutInflater 来访问 infoFrag.xml 文件中的 descriptionText TextView 字段。我可以通过我的 Toast 框看到正在设置的文本,但屏幕上显示的片段实际上并没有得到更新。

有人对这个问题有洞察力吗? 任何帮助将不胜感激!

EventInfo.java

public class EventInfo extends FragmentActivity implements
ActionBar.TabListener {

JSONObject obj;
GoogleMap map;
double latitude, longitude = 0;
String address = "";
String titleEvent = "";

private TextView descriptionText;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;

// tab titles
private String[] tabs = { "Event Info", "Google Map" };

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.eventinfo);

// Initialization:
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Adding Tabs
for (String tab_name : tabs) {
    actionBar.addTab(actionBar.newTab().setText(tab_name)
    .setTabListener(this));
}

try {
    obj = new JSONObject(getIntent().getStringExtra("json"));
    Toast.makeText(getApplicationContext(),
    "event = " + obj.getString("summary"), Toast.LENGTH_LONG)
    .show();
    address = obj.getString("location");
    titleEvent = obj.getString("summary");

} catch (JSONException e) {
    e.printStackTrace();
}

//I think the problem is somewhere in this code block!
LayoutInflater inflater = getLayoutInflater();
View descripText = inflater.inflate(R.layout.fragment_infofrag, null);
descriptionText = (TextView) descripText.findViewById(R.id.descriptionText);
descriptionText.setText("updated text"); //the textView gets updated here!
Toast.makeText(getApplicationContext(), "display: "+ descriptionText.getText(),     Toast.LENGTH_LONG).show(); //Toast box correctly displays, but the textView on the actual tab does not
}

TabsPagerAdapter.java

package com.uva.tabswipe.adapter;

import org.json.JSONException;
import org.json.JSONObject;

import com.uva.tabswipe.adapter.InfoFragment;
import com.uva.tabswipe.adapter.GMapFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);

    }    

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Tab with event info
            return new InfoFragment();
        case 1:
            // Tab with Google Map Fragment (this one is working fine)
            return new GMapFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 2;
    }

}

InfoFragment.java

package com.uva.tabswipe.adapter;...

public class InfoFragment extends Fragment {

    TextView descriptionText;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_infofrag, container, false);

        return rootView;
    }
}

fragment_infofrag.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E0EEEE"
    android:gravity="center" >

    <TextView
        android:id="@+id/descriptionText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#F88017"
        android:textSize="14sp"
        android:text="" />

</LinearLayout>

【问题讨论】:

  • 请输入 TabsPagerAdapter 代码。您在 TextView 中看不到文本,因为执行时 descripText = inflater.inflate(R.layout.fragment_infofrag, null);已创建不附加到您的布局的新视图。
  • @fisher3421 如果有帮助,我添加了 TabsPagerAdapter 以及相应的片段类和 xml 布局。如何将 descriptText 附加到我的片段布局?

标签: android android-fragments textview android-tabs


【解决方案1】:

如果您来自另一个片段,并尝试更新 onActivityResult 中的文本,那么下面的文本将起作用

      final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Do something after 500ms
                message.setText("Saved");
            }
        }, 500);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多