Parsing 使用ViewPager、PagerAdapter和AsyncTask的活动不';不要展示内容
我正在尝试构建一个类似于新安卓市场的活动,马赛克风格,在每个马赛克中列出信息。当用户向左或向右滑动时,他们将看到越来越多包含信息的马赛克。信息量是动态的,因为它是通过异步任务从XML文件加载的 我决定使用ViewPager/PagerAdapter来处理视图和控制分页。每个页面都有一个线性布局,与此链接中的图片类似: 马赛克中的每一个方块都是一个带有图像和文本视图的相对物 我遇到的问题是,显示的第一个视图始终为空,当我尝试更改页面时,会显示强制关闭错误。另外,当我调试时,我发现Parsing 使用ViewPager、PagerAdapter和AsyncTask的活动不';不要展示内容,parsing,android-viewpager,dynamic-content,Parsing,Android Viewpager,Dynamic Content,我正在尝试构建一个类似于新安卓市场的活动,马赛克风格,在每个马赛克中列出信息。当用户向左或向右滑动时,他们将看到越来越多包含信息的马赛克。信息量是动态的,因为它是通过异步任务从XML文件加载的 我决定使用ViewPager/PagerAdapter来处理视图和控制分页。每个页面都有一个线性布局,与此链接中的图片类似: 马赛克中的每一个方块都是一个带有图像和文本视图的相对物 我遇到的问题是,显示的第一个视图始终为空,当我尝试更改页面时,会显示强制关闭错误。另外,当我调试时,我发现PagesCont
PagesContentAdapter.java
中的instantiateItem()
方法根本没有输入!有什么好处
下面是我的活动(MosaicMenuActivity.java
):
异步任务由myPageContentLoader.java
完成。我从XML中获取所有数据,但未显示:
public class PageContentLoader extends AsyncTask<Void, Integer, Boolean> {
public static final String TAG = "PageContentLoader";
private static int SLEEPING_TIME = 2000;
// The location of the XML file
private static final String XML_PATH = "/data/nonius/menu/mosaic.xml";
// XML node keys used to parse xml
private static final String KEY_PAGE = "page";
private static final String KEY_PAGE_TEMPLATE = "template";
private static final String KEY_TILE = "tile";
// The parent activity. Used to lauch UI items
private MosaicMenuActivity activity;
// The content storage
private PagesContentAdapter content_adapter;
// The dialog that locks the user interaction
private LoaderDialog dialog;
// The array of content (in this demo, Pages!)
public ArrayList<Page> pages;
private String language;
public PageContentLoader(MosaicMenuActivity activity,
PagesContentAdapter content_adapter, String language) {
this.activity = activity;
this.pages = new ArrayList<Page>();
this.dialog = new LoaderDialog(this.activity);
this.content_adapter = content_adapter;
this.language = language;
}
public boolean loadXml() {
Log.d(TAG, "loadXml");
// Clean the pages array
this.pages = new ArrayList<Page>();
// Get the DOM element
Document doc = XmlHelper.getDomElement(XML_PATH);
// If the document is null
if (doc == null) {
// Return fail
return false;
}
// Get all the nodes with tag KEY_PAGE
NodeList page_nodelist = doc.getElementsByTagName(KEY_PAGE);
Log.d(TAG, "getElementsByTagName");
// looping through all item nodes <item>
for (int page_i = 0; page_i < page_nodelist.getLength(); page_i++) {
Page page = new Page();
Log.d(TAG, "Page " + page_i);
// Get the element of the node list
Element page_element = (Element) page_nodelist.item(page_i);
String page_template = page_element.getAttribute(KEY_PAGE_TEMPLATE);
page.setTemplate(page_template);
NodeList tile_nodelist = page_element
.getElementsByTagName(KEY_TILE);
for (int tile_i = 0; tile_i < tile_nodelist.getLength(); tile_i++){
// Create an empty tile
Tile tile = new Tile();
Element tile_element = (Element) tile_nodelist.item(tile_i);
tile.importXmlElement(tile_element);
page.addTile(tile);
Log.d(TAG, tile.toString());
}
this.pages.add(page);
}
// Everything went ok
return true;
}
protected void onPreExecute() {
this.dialog.setLoadMode(activity.getString(R.string.loading_message));
this.dialog.show();
}
protected Boolean doInBackground(Void... params) {
// Parse the xml file
return this.loadXml();
}
protected void onPostExecute(Boolean success) {
// If the loader succeeded
if (success) {
// Update the adapter
content_adapter.updateContent(pages);
// If the dialog is visible, hide it
if (dialog.isShowing()) {
dialog.dismiss();
}
}
// If the loader failed
else {
// Start the error dialog
dialog.setErrorMode(activity
.getString(R.string.loading_error_message));
}
}
}
感谢我能得到的任何帮助。感谢您抽出时间回顾此问题强>
public class PageContentLoader extends AsyncTask<Void, Integer, Boolean> {
public static final String TAG = "PageContentLoader";
private static int SLEEPING_TIME = 2000;
// The location of the XML file
private static final String XML_PATH = "/data/nonius/menu/mosaic.xml";
// XML node keys used to parse xml
private static final String KEY_PAGE = "page";
private static final String KEY_PAGE_TEMPLATE = "template";
private static final String KEY_TILE = "tile";
// The parent activity. Used to lauch UI items
private MosaicMenuActivity activity;
// The content storage
private PagesContentAdapter content_adapter;
// The dialog that locks the user interaction
private LoaderDialog dialog;
// The array of content (in this demo, Pages!)
public ArrayList<Page> pages;
private String language;
public PageContentLoader(MosaicMenuActivity activity,
PagesContentAdapter content_adapter, String language) {
this.activity = activity;
this.pages = new ArrayList<Page>();
this.dialog = new LoaderDialog(this.activity);
this.content_adapter = content_adapter;
this.language = language;
}
public boolean loadXml() {
Log.d(TAG, "loadXml");
// Clean the pages array
this.pages = new ArrayList<Page>();
// Get the DOM element
Document doc = XmlHelper.getDomElement(XML_PATH);
// If the document is null
if (doc == null) {
// Return fail
return false;
}
// Get all the nodes with tag KEY_PAGE
NodeList page_nodelist = doc.getElementsByTagName(KEY_PAGE);
Log.d(TAG, "getElementsByTagName");
// looping through all item nodes <item>
for (int page_i = 0; page_i < page_nodelist.getLength(); page_i++) {
Page page = new Page();
Log.d(TAG, "Page " + page_i);
// Get the element of the node list
Element page_element = (Element) page_nodelist.item(page_i);
String page_template = page_element.getAttribute(KEY_PAGE_TEMPLATE);
page.setTemplate(page_template);
NodeList tile_nodelist = page_element
.getElementsByTagName(KEY_TILE);
for (int tile_i = 0; tile_i < tile_nodelist.getLength(); tile_i++){
// Create an empty tile
Tile tile = new Tile();
Element tile_element = (Element) tile_nodelist.item(tile_i);
tile.importXmlElement(tile_element);
page.addTile(tile);
Log.d(TAG, tile.toString());
}
this.pages.add(page);
}
// Everything went ok
return true;
}
protected void onPreExecute() {
this.dialog.setLoadMode(activity.getString(R.string.loading_message));
this.dialog.show();
}
protected Boolean doInBackground(Void... params) {
// Parse the xml file
return this.loadXml();
}
protected void onPostExecute(Boolean success) {
// If the loader succeeded
if (success) {
// Update the adapter
content_adapter.updateContent(pages);
// If the dialog is visible, hide it
if (dialog.isShowing()) {
dialog.dismiss();
}
}
// If the loader failed
else {
// Start the error dialog
dialog.setErrorMode(activity
.getString(R.string.loading_error_message));
}
}
}
public class PagesContentAdapter extends PagerAdapter {
public static final String TAG = "PagesContentAdapter";
// The list context
private Context context;
// The content array
public ArrayList<Page> pages;
// The system language
private String language;
/**
* Class constructor
*
* @param context
* The context used to build the ouput views
*/
public PagesContentAdapter(Context context, String language) {
this.context = context;
// Create an empty pages array
this.pages = new ArrayList<Page>();
this.language = language;
}
/**
* Update the Adapter pages array and notify the parent
* that the data was modified
* @param pages
*/
public void updateContent(ArrayList<Page> pages) {
this.pages = pages;
//this.notifyDataSetChanged();
}
public int getCount() {
return pages.size();
}
public Object getItem(int position) {
return pages.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public Object instantiateItem(View page_view, int position) {
Page page = new Page();
page = pages.get(position);
LayoutInflater page_layout_inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearlayout_page = (LinearLayout) page_layout_inflater.inflate(R.layout.page_template_5icons_v1, null);
for(int i=0; i<5; i++) {
linearlayout_page.addView(layoutTile(linearlayout_page, page, i));
}
((ViewPager) page_view).addView(linearlayout_page, 0);
return linearlayout_page;
}
public RelativeLayout layoutTile(LinearLayout linearlayout_page, Page page, int index) {
RelativeLayout relativelayout_tile = null;
switch(index) {
case 0: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_big_1);
case 1: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_1);
case 2: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_2);
case 3: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_3);
case 4: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_4);
}
// Inflate it in a layout xml
LayoutInflater tile1_layout_inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
tile1_layout_inflater.inflate(R.layout.tile, relativelayout_tile);
// Get the textview from the inflated layout
TextView textview_tail = (TextView) relativelayout_tile.findViewById(R.id.textview_tile);
Tile tile = page.getTiles().get(index);
// Set its text
textview_tail.setText(tile.getDescription());
return relativelayout_tile;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
}