我正在试图弄清楚在运行UI和设置ViewPager适配器之前如何从服务器下载内容。我的服务器上有JSON文件,应用程序正确读取,但我无法更新ViewPager。
理想情况下,我想做的是制作一个对话框,上面写着“请等待,我们正在检查是否有任何新内容”,带有循环加载的东西(只是为了表明它正在工作),如果是的话 - 来下载它并使内容新鲜,如果不使用最后的内容。
现在看来就是这样。它按原样工作 - 我应该从哪里拿到它?
public class MainActivity extends Activity {
MediaPlayer mp;
ViewPager viewPager;
PagerAdapter adapter;
//int[] sounds;
int[] g;
String[] title;
int[] description;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_main);
// Generate data
title = new String[] { "A1", "A2", "A3","A4" };
description = new int[] { R.string.a1, R.string.a2, R.string.a3, R.string.a4 };
gun = new int[] {R.drawable.obr4,R.drawable.obr4,R.drawable.obr4,R.drawable.obr4};
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(MainActivity.this, g, title,
description);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
if (VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
viewPager.setPageTransformer(true, new DepthPageTransformer());
}
new GetUpdate().execute();
}
class GetUpdate extends AsyncTask<Void, Void, Void>{
JSONObject jobj = null;
@Override
protected Void doInBackground(Void... params) {
//JSON
JSONParser jparser = new JSONParser();
JSONObject data = jparser.getJSONFromUrl("http://myserver.com/file.json");
String title2 = "";
try {
jobj = data.getJSONObject("party2");
title2 = jobj.getString("title");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//title = new String[] { title2,title2,title2,title2}; <- commented 'cause doesn't work
return null;
}
}
答案 0 :(得分:1)
获取新的JSON
之后,只需更新适配器的源列表,并在适配器对象上执行'notifyDataSetChanged()`,如下所示。
public class MainActivity extends Activity {
MediaPlayer mp;
ViewPager viewPager;
PagerAdapter adapter;
//int[] sounds;
int[] g;
String[] title;
int[] description;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_main);
// Generate data
title = new String[] { "A1", "A2", "A3","A4" };
description = new int[] { R.string.a1, R.string.a2, R.string.a3, R.string.a4 };
gun = new int[] {R.drawable.obr4,R.drawable.obr4,R.drawable.obr4,R.drawable.obr4};
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(MainActivity.this, g, title,
description);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
if (VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
viewPager.setPageTransformer(true, new DepthPageTransformer());
}
new GetUpdate().execute();
}
class GetUpdate extends AsyncTask<Void, Void, Void>{
JSONObject jobj = null;
@Override
protected Void doInBackground(Void... params) {
//JSON
JSONParser jparser = new JSONParser();
JSONObject data = jparser.getJSONFromUrl("http://myserver.com/file.json");
String title2 = "";
try {
jobj = data.getJSONObject("party2");
title2 = jobj.getString("title");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Change titles to ArrayList for better usage.
title[4] = title2;
adapter.notifyDataSetChanged();
return null;
}
}
答案 1 :(得分:0)
您可以覆盖AsyncTask的onPostExecute()方法,将更新逻辑放在那里