我目前正在为android listview解析xml。 我能够解析并获取我的列表视图的数据。 但是,如果我想从该XML获取特定数据,该怎么办?嗯像执行 东西在SQL“select * from [table] where id = 1”。 类似的东西......我怎么用我的XML来做到这一点? 谢谢在Android中为ListView解析XML
0
A
回答
0
您应该只解析xml文件一次,并从中创建一个数组或对象列表。一旦你拥有了内存中的所有东西,你就可以使用这个数组来填充你的列表,使用ArrayAdapter。数组中的位置可以用作你的id。如果你宁愿让对象的属性成为id而不是位置,那么你可以相应地对数组进行排序。
1
public class XMLParsing_DisplayListviewActivity extends Activity {
String res1 = null;
InputStream is = null;
StringBuilder sb = null;
public String[] mCD;
public String[] mTitle;
public String[] mArtist;
public String[] mCountry;
public String[] mCompany;
public String[] mPrice;
public String[] mYear;
ListView listview;
ProgressDialog mDialog = null; // thread code
private Runnable viewOrders; // thread code
private Thread thread1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview=(ListView)findViewById(R.id.listview);
mDialog = new ProgressDialog(this); // thread code
mDialog.setMessage("Please wait...");
viewOrders = new Runnable(){
public void run(){
load();
runOnUiThread(returnRes);
}
};
thread1 = new Thread(null, viewOrders, "Background"); // thread
thread1.start(); // thread code
mDialog.show();
}
private Runnable returnRes = new Runnable(){
public void run(){
mDialog.cancel();
for(int i=0;i<mCD.length;i++){
System.out.println("======================");
System.out.println("Title :- "+mTitle[i]);
System.out.println("Artist :- "+mArtist[i]);
System.out.println("Country :- "+mCountry[i]);
System.out.println("Company :- "+mCompany[i]);
System.out.println("Price :- "+mPrice[i]);
System.out.println("Year :- "+mYear[i]);
}
listview.setAdapter(new CustomAdapter(XMLParsing_DisplayListviewActivity.this));
}
};
public void load() {
//mLoading = true;
Document doc = null;
HttpURLConnection urlConnection = null;
URL url = null;
try {
//url = new URL(urlString.toString());
url = new URL("http://www.xmlfiles.com/examples/cd_catalog.xml");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(urlConnection.getInputStream());
} catch (Exception e) {
System.out.println("e"+e.toString());
}
try {
NodeList nodes = doc.getElementsByTagName("CD");
// iterate the employees
mCD=new String[nodes.getLength()];
mTitle=new String[nodes.getLength()];
mArtist=new String[nodes.getLength()];
mCountry=new String[nodes.getLength()];
mCompany=new String[nodes.getLength()];
mPrice=new String[nodes.getLength()];
mYear=new String[nodes.getLength()];
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList cd = element.getElementsByTagName("CD");
Element line = (Element) cd.item(0);
mCD[i]=getCharacterDataFromElement(line);
NodeList title = element.getElementsByTagName("TITLE");
line = (Element) title.item(0);
mTitle[i]=getCharacterDataFromElement(line);
//System.out.println("catcatcatmidmidmidmidName: " + getCharacterDataFromElement(line));
NodeList artist = element.getElementsByTagName("ARTIST");
line = (Element) artist.item(0);
mArtist[i]=getCharacterDataFromElement(line);
//System.out.println("salary: " + getCharacterDataFromElement(line));
NodeList country = element.getElementsByTagName("COUNTRY");
line = (Element) country.item(0);
mCountry[i]=getCharacterDataFromElement(line);
NodeList company = element.getElementsByTagName("COMPANY");
line = (Element) company.item(0);
mCompany[i]=getCharacterDataFromElement(line);
NodeList price = element.getElementsByTagName("PRICE");
line = (Element) price.item(0);
mPrice[i]=getCharacterDataFromElement(line);
NodeList year = element.getElementsByTagName("YEAR");
line = (Element) year.item(0);
mYear[i]=getCharacterDataFromElement(line);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "?";
}
public class CustomAdapter extends BaseAdapter {
private Context mContext;
Application app;
private LayoutInflater inflater=null;
public CustomAdapter(Context c) {
mContext = c;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return mTitle.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.list, null);
TextView txt=(TextView)vi.findViewById(R.id.txtview_title);
TextView txt1=(TextView)vi.findViewById(R.id.txtview_artist);
TextView txt2=(TextView)vi.findViewById(R.id.txtview_country);
TextView txt3=(TextView)vi.findViewById(R.id.txtview_company);
TextView txt4=(TextView)vi.findViewById(R.id.txtview_price);
TextView txt5=(TextView)vi.findViewById(R.id.txtview_year);
txt.setText("Title : " + mTitle[position]);
txt1.setText("Artist : " + mArtist[position]);
txt2.setText("Country : " + mCountry[position]);
txt3.setText("Company : " + mCompany[position]);
txt4.setText("Price : " + mPrice[position]);
txt5.setText("Year : " + mYear[position]);
return vi;
};
}
}
+0
没有任何解释。只是代码的转储。即使有经验的android开发人员也知道如何处理这个问题,但为业余开发人员提供一些背景并不会带来不便。 – 2015-10-27 12:12:38
相关问题
- 1. Android SOAP XML解析ListView错误
- 2. 在Android中解析XML DOMDOWNE
- 3. 在Android中解析XML流
- 4. xml在android中的解析
- 5. 在android中解析XML
- 6. xml在android中解析
- 7. 在android中的XML解析
- 8. Xml解析在android
- 9. Xml解析在android
- 10. Android - 将XML解析为ArrayList
- 11. ANDROID:解析XML
- 12. Android XML解析
- 13. XML解析Android
- 14. 如何解析XML Android中
- 15. Android中的XML解析,IXMLDOMDocument
- 16. Android中的XML解析[Java]
- 17. SAX解析XML Android中
- 18. Android XML解析器
- 19. Android的XML解析
- 20. 用Android解析XML
- 21. android AsyncTask xml解析
- 22. Android的XML解析
- 23. Android XML解析AsyncTask
- 24. 在Android中解析XML中的注释
- 25. 如何在Android中解析这个XML?
- 26. 使用Xpath在Android中解析XMl
- 27. 在android中使用SAX的XML解析
- 28. 在android中的高级XML解析
- 29. 在Android中解析本地XML文件
- 30. 在android中解析xml文件
我的意思是我有我自己的id..like我一定要找只具有相同所有数据.. –
elL
2013-02-22 06:12:53
的'id'在这种情况下,你是怎么想自己识别数据。它不需要是一个外部id或任何东西,它只是一种使'getItemId(position)'返回一些对你有意义的方法,你可以在稍后使用它来访问对象。 – ebarrenechea 2013-02-22 17:35:24
现在我已经切换到JSON .. 是一样的概念吗? – elL 2013-02-23 04:29:21