首先我很抱歉我的英语不好。
这是我的目标;
我需要从我的链接中读取 xml(实际上是从 Web 服务,但我的主机有一些问题,我使用直接 xml 链接,因为我的 Web 服务返回数据)。然后我需要将这些数据写入listView。
这是我的问题;
我从网络链接中读取了那个 xml,我认为解析了 xml。我这样说是因为当我调试项目时,我可以在我的 GettersSetters 变量上看到一些数据。但是当我将这些数据发送到我的适配器类时,我猜没有任何动作。因为我在我的 listView 上看不到任何更改。
这是我的 MainActivity.java
public class MainActivity extends Activity {
XMLGettersSetters data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL("http://utkuyegen.com/dersProgramiListele.xml");
XMLHandler myXMLHandler = new XMLHandler();
xmlR.setContentHandler(myXMLHandler);
xmlR.parse(new InputSource(url.openStream()));
} catch (Exception e) {
Log.e("xmlParser", e.getMessage());
System.out.println(e);
}
data = XMLHandler.data;
System.out.println("data = XMLHandler.data");
try {
ListView lv = (ListView) findViewById(R.id.listView1);
System.out.println("listView tanımlandı");
dataAdapter adapter = new dataAdapter(this, R.layout.listview_row,
data.getDersID(), data.getDersAdi());
System.out.println("adapter tanımlandı");
lv.setAdapter(adapter);
System.out.println("adapter listView 'e atandı");
// TextView textView1 = (TextView) findViewById(R.id.textView1);
// textView1.setText(data.getDersID().get(2));
System.out.println("onCreate son satır");
} catch (Exception e) {
Log.d("tag", e.getMessage());
}
}
public class dataAdapter extends ArrayAdapter<XMLGettersSetters> {
// XMLGettersSetters items;
ArrayList<String> _dID, _dAdi;
public dataAdapter(Context context, int textViewResourceId,
ArrayList<String> dID, ArrayList<String> dAdi) {
super(context, textViewResourceId);
this._dID = dID;
this._dAdi = dAdi;
Log.d("da", "1");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listview_row, null);
Log.d("da", "5");
}
Log.d("da", "2");
TextView dersID = (TextView) convertView.findViewById(R.id.dersID);
TextView dersAdi = (TextView) convertView
.findViewById(R.id.dersAdi);
Log.d("da", "3");
dersID.setText(_dID.get(position));
dersAdi.setText(_dAdi.get(position));
Log.d("da", "4");
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
和 XMLGettersSetters.java
public class XMLGettersSetters {
private ArrayList<String> dersID = new ArrayList<String>();
private ArrayList<String> dersAdi = new ArrayList<String>();
public XMLGettersSetters(ArrayList<String> dersID, ArrayList<String> dersAdi) {
this.dersID = dersID;
this.dersAdi = dersAdi;
}
public XMLGettersSetters() {
}
public String getDersID_tek(int i) {
return dersID.get(i);
}
public ArrayList<String> getDersID() {
return dersID;
}
public void setDersID(String dersID) {
this.dersID.add(dersID);
}
public String getDersAdi_tek(int i) {
return dersAdi.get(i);
}
public ArrayList<String> getDersAdi() {
return dersAdi;
}
public void setDersAdi(String dersAdi) {
this.dersAdi.add(dersAdi);
}
}
和 XMLHandler.java
public class XMLHandler extends DefaultHandler {
String elementValue = null;
Boolean elementOn = false;
public static XMLGettersSetters data = null;
public static XMLGettersSetters getXMLData() {
return data;
}
public static void setXMLData(XMLGettersSetters data) {
XMLHandler.data = data;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
elementOn = true;
if (localName.equals("ArrayOfDersProgrami"))
{
data = new XMLGettersSetters();
} else if (localName.equals("dersProgrami"))
{
/**
* We can get the values of attributes for eg. if the CD tag had an attribute( <CD attr= "band">Akon</CD> )
* we can get the value "band". Below is an example of how to achieve this.
*
* String attributeValue = attributes.getValue("attr");
* data.setAttribute(attributeValue);
*
* */
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
elementOn = false;
/**
* Sets the values after retrieving the values from the XML tags
* */
if (localName.equalsIgnoreCase("DersID"))
data.setDersID(elementValue);
if (localName.equalsIgnoreCase("DersAdi"))
data.setDersAdi(elementValue);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (elementOn) {
elementValue = new String(ch, start, length);
elementOn = false;
}
}
}
顺便说一句,我对我的代码和调试有所了解。
当我调试这段代码时,我发现我无法理解数据。
这是我的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDersProgrami xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<dersProgrami>
<DersID>0</DersID>
<DersAdi>0</DersAdi>
</dersProgrami>
<dersProgrami>
<DersID>1</DersID>
<DersAdi>1</DersAdi>
</dersProgrami>
<dersProgrami>
<DersID>2</DersID>
<DersAdi>4</DersAdi>
</dersProgrami>
<dersProgrami>
<DersID>3</DersID>
<DersAdi>9</DersAdi>
</dersProgrami>
<dersProgrami>
<DersID>4</DersID>
<DersAdi>16</DersAdi>
</dersProgrami>
</ArrayOfDersProgrami>
如您所见,有五个数据,但正如我在调试时所说,我在数组中看到 12 个数据。我的范围数据变量“data = XMLHandler.data;” 我看到两个arrayList,dersAdi 和dersID。然后我作用域 dersAdi,我看到数组 = Object[12],modCount = 5,大小 = 5。然后我作用域数组 = Obj .. 我看到 [0,1,4,9,16,null,null,null,null ,空,空,空]。
那什么意识?我的 xml 中有 5 个数据,但有 12 个数据。
我想要什么;我需要一些建议,而且我已经接近最后期限了。
这个项目是我的季节