我已经开始为 Android 编程(从 iOS),当我在新的项目向导中选择 Activity+Scrollable tabs 选项时,我使用 ADT 为我生成的样板代码。在下面的代码中,我在评论中标记了相同的内容。
场景是我在 editText 中输入文本,按下按钮并从 web 服务中获取一些数据。我希望将这些数据放在视图寻呼机内的适当片段(有 3 个片段)中。我正在获取数据,并且可以在滑动时将其设置在 Fragment 2 和 Fragment 3 上,但是一旦 AsycTask 完成,与 Fragment 1 相关的数据就不会被设置。但是,在刷到片段 3 后,当我回到片段 1 时,数据就在那里。
这是我的代码:
活动
public class XXXX extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
//My additions to the boiler plate source generated by ADT
public ArrayList<String> p1T,p2T,p3T,p4T;
private EditText editText;
private Button button;
private boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.konjugation);
mSectionsPagerAdapter = new SectionsPagerAdapter (getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
//My additions to the boiler plate source below
flag=false;
p1T = new ArrayList<String>(6);
p2T = new ArrayList<String>(6);
p3T = new ArrayList<String>(6);
p4T = new ArrayList<String>(6);
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String url = editText.getText().toString();
if(!url.isEmpty()) {
url="http://.../?q="+editText.getText().toString();
PAsyncTask task = new PAsyncTask();
task.execute(url);
}
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
@Override
public void onPageSelected(int pos) {
if(flag==true) {
if(pos==0) DummySectionFragment.setText(p4T,p1T);
if(pos==1) DummySectionFragment.setText(p4T,p2T);
if(pos==2) DummySectionFragment.setText(p4T,p3T);
}
else {
Log.d("ts","data not arrived");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.konjugation, menu);
return true;
}
寻呼机适配器
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new DummySectionFragment();
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1);
case 1:
return getString(R.string.title_section2);
case 2:
return getString(R.string.title_section3);
}
return null;
}
@Override
public void notifyDataSetChanged() {
int pos = mViewPager.getCurrentItem();
if(pos==0) DummySectionFragment.setText(p4T,p1T);
else if(pos==1) DummySectionFragment.setText(p4T,p2T);
else if(pos==2) DummySectionFragment.setText(p4T,p3T);
}
}
异步任务
private class PAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params)
{
String response = "";
for (String url : params) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
protected void onPostExecute(String result)
{
try {
p1T.clear();
p2T.clear();
p3T.clear();
//do something very important with the result here
flag=true;
//hoping that somehow the viewpager adapter will refresh the view
mSectionsPagerAdapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Viewpager 的片段
public static class DummySectionFragment extends Fragment {
private static TextView dummyTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_konjugation_dummy, container, false);
dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
return rootView;
}
public static void setText(ArrayList<String> s, ArrayList<String> t) {
try {
DummySectionFragment.dummyTextView.setText("");
for(int i=0;i<s.size();i++) {
DummySectionFragment.dummyTextView.append(s.get(i)+" "+t.get(i)+"\n\n");
}
} catch(NullPointerException e) {
e.printStackTrace();
}
}
}
}
在这里阅读了一些主题后,我感到有点失落。请帮忙 :-(