public class MainActivity extends Activity {
ListView list;
EditText name,number;
Button store,display,update;
ArrayList<String> fetchData = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.pname);
number=(EditText)findViewById(R.id.pnumber);
store=(Button)findViewById(R.id.bSignup);
display=(Button)findViewById(R.id.bShow);
update=(Button)findViewById(R.id.bUpdate);
list=(ListView)findViewById(R.id.list);
try{
Time now = new Time();
now.setToNow();
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("user");
doc.appendChild(rootElement);
// staff elements
Element staff = doc.createElement("details");
rootElement.appendChild(staff);
// set attribute to staff element
Attr attr = doc.createAttribute("id");
attr.setValue("1");
staff.setAttributeNode(attr);
// shorten way
// staff.setAttribute("id", "1");
// firstname elements
Element firstname = doc.createElement("firstname");
firstname.appendChild(doc.createTextNode("manjeet"));
staff.appendChild(firstname);
// lastname elements
Element lastname = doc.createElement("lastname");
lastname.appendChild(doc.createTextNode("singh"));
staff.appendChild(lastname);
// nickname elements
Element nickname = doc.createElement("nickname");
nickname.appendChild(doc.createTextNode("mr. singh"));
staff.appendChild(nickname);
// salary elements
Element salary = doc.createElement("salary");
salary.appendChild(doc.createTextNode("100000"));
staff.appendChild(salary);
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/data/data/com.example.xmlparsing/files/Register.xml"));
transformer.transform(source, result);
System.out.println("File saved!");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
Intent i = new Intent(MainActivity.this,XMLConcat.class);
startActivity(i);
display.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this,XMLConcat.class);
startActivity(i);
}
});
}
public class XMLConcat extends Activity{
ListView list;
ArrayList<String> fetchData = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.display_list);
list=(ListView)findViewById(R.id.list);
try {
File fXmlFile = new File("/data/data/com.example.xmlparsing/files/Register.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("details");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
//at console
System.out.println("Staff id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
fetchData.add(eElement.getAttribute("id"));
fetchData.add(eElement.getAttribute("firstname"));
fetchData.add(eElement.getAttribute("lastname"));
fetchData.add(eElement.getAttribute("nickname"));
fetchData.add(eElement.getAttribute("salary"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
setListAdapter();
}
private void setListAdapter(){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item,fetchData);
list.setAdapter(adapter);
}
}
我想在listview中检索xml数据。这个程序只在控制台中显示输出,但我想在listview中显示输出。我正在学习如何在Android中解析xml文档。所以对它没有多少了解。请指导我。
答案 0 :(得分:2)
看看这些教程:
http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
答案 1 :(得分:0)
像这样使用自定义适配器。试试这个。
public class Adapter extends BaseAdapter {
LayoutInflater inflater;
ArrayList<DbResultBean> beanList;
DbResultBean bean;
Holder mHolder;
Handler handler;
public Adapter(Context context,
ArrayList<DbResultBean> beanList,Handler handler) {
inflater = LayoutInflater.from(context);
this.beanList = beanList;
this.handler=handler;
}
@Override
public int getCount() {
return beanList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.adapter_result_listitem, null, false);
mHolder = new Holder();
mHolder.mtxt_exam_dt=(TextView) view.findViewById(R.id.mTxt_ExamDt);
mHolder.mtxt_marks=(TextView) view.findViewById(R.id.mTxt_MarksObtn);
mHolder.mtxt_subject=(TextView) view.findViewById(R.id.mTxt_Subject);
mHolder.mtxtCourse=(TextView) view.findViewById(R.id.mTxt_Course);
mHolder.mtxtCollege=(TextView) view.findViewById(R.id.mTxt_College);
view.setTag(mHolder);
}else{
mHolder=(Holder) view.getTag();
}
bean=beanList.get(position);
mHolder.mtxt_exam_dt.setText(bean.getExam_date());
mHolder.mtxt_marks.setText(bean.getMark_obtain());
mHolder.mtxt_subject.setText(bean.getSubject());
mHolder.mtxtCourse.setText(bean.getCourse());
mHolder.mtxtCollege.setText(bean.getCollege());
return view;
}
public class Holder {
TextView mtxt_exam_dt;
TextView mtxt_marks;
TextView mtxt_subject;
TextView mtxtCourse;
TextView mtxtCollege;
}
}