【发布时间】: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