我正在解析XML并尝试在列表视图中传播它。
我正在使用以下代码
主要活动,
public class MainActivity extends ListActivity {
ArrayList<String> xmlList=new ArrayList<String>();
ArrayList<String> xmlList1=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
InputStream is=getResources().openRawResource(R.raw.sample);
DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc=builder.parse(is, null);
NodeList nodes=doc.getElementsByTagName("id");
NodeList nodes1=doc.getElementsByTagName("Name");
if(nodes!=null && nodes.getLength() >0)
{
xmlList.clear();
xmlList1.clear();
int len=nodes.getLength();
for(int i=0;i<len;i++)
{
Node node=nodes.item(i);
Node node1=nodes1.item(i);
xmlList.add(node.getTextContent());
}
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), ""+e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
CustomList adapter=new CustomList(MainActivity.this,xmlList,xmlList1);
setListAdapter(adapter);
CustomList.java
public class CustomList extends ArrayAdapter<String> {
Context context;
private final ArrayList<String> mainText;
private final ArrayList<String> subText1;
public CustomList(Context context, ArrayList<String> mainText, ArrayList<String> subText1) {
super(context,R.layout.custumlist);
// TODO Auto-generated constructor stub
this.context=context;
this.mainText=mainText;
this.subText1=subText1;
}
@Override
public View getView(int position, View view, ViewGroup parent)
{
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.custumlist, null);
TextView main=(TextView)view.findViewById(R.id.mainText);
TextView sub1=(TextView)view.findViewById(R.id.subText1);
main.setText(mainText.toString());
sub1.setText(subText1.toString());
return view;
}
}
但它没有显示任何内容,也没有张贴任何日志猫。 我认为在改变视图方面存在一些问题。小问题但无法找到。请帮助我。
日Thnx
答案 0 :(得分:5)
覆盖getCount
CustomList
方法
@Override
public int getCount()
{
return mainText.size();
}
OR
从{/ 1>更改super
构建函数中的CustomList
调用
super(context, R.layout.custumlist);
到
super(context, R.layout.custumlist,mainText);
答案 1 :(得分:2)
在您的CustomList中放置代码:
@Override
public int getCount() {
// TODO Auto-generated method stub
return mainText.size();
}