我正在使用fragment
和viewpager
撰写一个简单的应用来显示来自webservice
的数据。问题是viewpager
第一次创建3 fragments
,但数据仅在最后fragment
上查看。
这是我的活动:
public class LineDetailTabs extends FragmentActivity {
//declare some variables here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_detail_tabs);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
PlaceholderFragment hf = new PlaceholderFragment();
fragmentTransaction.replace(R.id.container, hf);
fragmentTransaction.commit();
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(index);
}
这是我的适配器, list_nodep 是一个String数组,包含许多deparments的id。 计数是部门数量,也会创建标签数量。
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
noDep = list_nodep[position];
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
//put some agurments
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return count;
}
}
我的片段在这里,所有片段都有相同的布局xml文件。
public static class PlaceholderFragment extends Fragment {
public static View rootView;
public static TextView tvHeader;
public static TextView tvPO;
public static TextView tvLot;
// many textviews
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
activity = (LineDetailTabs) getActivity();
loadData(noDep, date, username, dbIP, dbName, dbUsername, dbPassword);
rootView = inflater.inflate(R.layout.fragment_line_detail_tabs, container, false);
tvHeader = (TextView) rootView.findViewById(R.id.txtHeader);
tvPO = (TextView) rootView.findViewById(R.id.txtPO);
tvLot = (TextView) rootView.findViewById(R.id.txtLOT);
// many textviews remaining...
return rootView;
}
public static void loadData(String aNoDep, String aDate, String aUsername, String aDbIP, String aDbName, String aDbUser, String aDbPass) {
RequestParams params = new RequestParams();
//put params
invokeWS(params);
}
public static void invokeWS(final RequestParams params) {
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://...", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String response) {
try {
// JSON Object
JSONObject obj = new JSONObject(response);
// When the JSON response has status boolean value assigned with true
JSONArray jArr = obj.getJSONArray("list");
// Display info detail on screen
obj = jArr.getJSONObject(0);
tvHeader.setText = obj.getString("NAME_DEP");
tvPO.setText = obj.getString("NO_PO");
tvLot.setText = obj.getString("NO_LOT");
//set text for all textviews
}
// When the response returned by REST has Http response code other than '200'
@Override
public void onFailure(int statusCode, Throwable error,
String content) {
// When Http response code is '404'
if (statusCode == 404) {
Toast.makeText(activity.getApplicationContext(), Constants.ERR_CODE_404, Toast.LENGTH_SHORT).show();
}
// When Http response code is '500'
else if (statusCode == 500) {
Toast.makeText(activity.getApplicationContext(), Constants.ERR_CODE_500, Toast.LENGTH_SHORT).show();
}
// When Http response code other than 404, 500
else {
Toast.makeText(activity.getApplicationContext(), Constants.ERR_CODE_, Toast.LENGTH_SHORT).show();
}
}
});
}
}
调试我的应用时,我看到fragment
onCreateView
连续运行3次,之后光标跳转到onSuccess
,所以只显示最后fragment
个数据。我想我把loadData()
方法或setText
放在了错误的地方。请帮忙。
答案 0 :(得分:0)
好的,最后我修好了。这是我的错误,我没有将params从活动传递到碎片。 把para放在这里
span
然后在onCreateView()中获取这些参数,并调用loadData()方法。
Map<Point, BufferedImage> loadedImages = new ConcurrentHashMap<>();
Deque<Point> lastUsed = new ConcurrentLinkedDeque<>();
int getRGB(double tileX, double tileY) {
Point point = new Point((int) tileX, (int) tileY);
if (!loadedImages.containsKey(point)) {
loadedImages.put(point, ImageIO.read(new File("R:\\tiles\\22\\" + point.y + "_" + point.x + ".jpg")));
lastUsed.addLast(point);
}
BufferedImage img = loadedImages.get(point);
if (loadedImages.size() > 1000) {
loadedImages.remove(lastUsed.pollFirst());
}
//do stuff with img
}