textView没有使用更新的文本刷新片段(Android开发)

时间:2014-03-23 04:31:02

标签: android android-fragments textview android-tabs

在我的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>

1 个答案:

答案 0 :(得分:0)

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

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