我从 XML 文件中获取数据,如下所示:-
<posts>
<page id="1">
<title>
<![CDATA[ Tile no. 1 ]]>
</title>
<smallimage>
http://www.xyz.com/right.jpg
</smallimage>
</page>
<page id="2">
<title>
<![CDATA[ Tile no. 2 ]]>
</title>
</page>
<page id="3">
<title>
<![CDATA[ Tile no. 3 ]]>
</title>
<smallimage>
http://www.xyz.com/left.jpg
</smallimage>
</page>
</posts>
这是我的 XML 示例,我在其中获取元素,并且在单个 url 中获取 10 个页面,其中存在 10 个,但没有预设 10 个。它可能是 3 或 6 或 1。
在我的代码中,我使用 SAX 解析器来解析这个 XML 文件,我正在获取我的数据并能够在列表视图上显示,但我能够用它们显示图像。
为了显示图像,我使用 LazlyList 示例(ImageLoader 类)。
下面是我的代码:-
MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
static final String URL = "http://www.xyz.com/api.php?page_id=1";
ItemList itemList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
XMLParser parser = new XMLParser();
String XML = parser.getXmlFromUrl(URL);
System.out.println("This XML is ========>"+XML);
try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
ByteArrayInputStream is = new ByteArrayInputStream(XML.getBytes());
xr.parse(new InputSource(is));
}
catch(Exception e)
{
}
itemList = MyXMLHandler.itemList;
ArrayList<String> listItem= itemList.getTitle();
ArrayList<String> listManu = itemList.getSmallimages();
System.out.println("(ListItem)=======>"+listItem);
System.out.println("(ListManu)=======>"+listManu);
// String ar[];
Object obj[]=listManu.toArray();
String[] stringArray=Arrays.asList(obj).toArray(new String[obj.length]);
System.out.println("Array to String"+Arrays.toString(stringArray));
// ar=(String[]) listManu.toArray();
ListView lview = (ListView) findViewById(R.id.listview1);
myAdapter adapter = new myAdapter(this, listItem, stringArray);
lview.setAdapter(adapter);
}
我的适配器.java
public class myAdapter extends BaseAdapter
{
ArrayList<String> listTitle;
String [] data;
ImageLoader imageLoader;
Activity activity;
public myAdapter(Activity activity, ArrayList<String> listTitle, String[] d) {
super();
this.listTitle = listTitle;
this.data = d;
this.activity = activity;
this.imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder
{
TextView txtViewTitle;
ImageView image;
}
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder title;
LayoutInflater inflater = activity.getLayoutInflater();
if(view==null)
{
view = inflater.inflate(R.layout.lview_row, null);
title = new ViewHolder();
title.txtViewTitle = (TextView) view.findViewById(R.id.txtItem);
title.image = (ImageView) view.findViewById(R.id.list_image);
view.setTag(title);
}
else
{
title = (ViewHolder) view.getTag();
}
title.txtViewTitle.setText(listTitle.get(position));
imageLoader.DisplayImage(data[position], title.image);
return view;
}
}
\
当我使用 return data.length 时,我得到的只是图像和平铺。的图像存在,因为 String [] 只存储图像而不是空格......
public int getCount() {
return data.length;
但是当我将 data.length 更改为 listTitle.size()
public int getCount() {
return listTitle.size();
获取空指针异常我正在使用 listTilte.size() 因为它给出了完整的列表......
任何人请建议我如何解决这个问题并在列表视图中获取图像和完整的标题数据以及图像存在的地方只有我会得到带有标题的图像,否则只有标题显示在列表视图中......
先谢谢了。