我最近一直在研究Android ListViews,但我遇到了一些问题。我查看了一些教程,并获得了最基本的ListViews工作正常。但现在我想创建一个显示一系列对象的ListView。我以为我做的一切都是正确的。我的代码不会产生任何错误,但我想创建的列表根本不会显示。显然,我不确定为什么。
此应用程序的目的是编制气象站和机场列表并显示信息(例如名称,ID#,坐标等) - 所有这些信息都是从XML文档中解析出来并包含在Arraylist中(内置)通过一个单独的类与适当的构造函数/ getter / setter)。基本上我得到一个站列表 - 然后从该列表 - 一个设置为适配器的站名列表。我的ListView应该只显示每个电台的名称,但无济于事。
我测试了我的Parser和My Arrays。这一切都有效,只是没有显示。根据所有教程,我的逻辑应该正确到我的适配器。有没有人有什么建议?我觉得我已经筋疲力尽了所有的解决方案。我的代码发布在下面:
public class MainActivity extends Activity {
//local variables
String station_id;
String state;
String station_name;
double latitude;
double longitude;
String html_url;
//List of Station Objects
public ArrayList<Station> StationList = new ArrayList<Station>();
Method for DOM Parser
public void readXML(){
try {
//new xml file and Read
File file1 = new File("src/fl_wx_index3.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file1);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("station");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE){
final Element first = (Element) node;
station_id = first.getElementsByTagName("station_id").item(0).getTextContent();
state = first.getElementsByTagName("state").item(0).getTextContent();
station_name = first.getElementsByTagName("station_name").item(0).getTextContent();
latitude = Double.parseDouble(first.getElementsByTagName("latitude").item(0).getTextContent());
longitude = Double.parseDouble(first.getElementsByTagName("longitude").item(0).getTextContent());
html_url = first.getElementsByTagName("html_url").item(0).getTextContent();
//iterate thru list, returning names of each airport
StationList.add(new Station(station_id, state, station_name, latitude, longitude, html_url));
}
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//object for method call to read XML document
MainActivity activity1 = new MainActivity();
activity1.readXML();
final ListView listview = (ListView) findViewById(R.id.listView1);
//List to contain Weather station Names
final ArrayList<String> nameList = new ArrayList<String>();
for (int i = 0; i < StationList.size(); ++i) {
nameList.add(StationList.get(i).getStationName());
}
//Array adapter
final StableArrayAdapter adapter = new StableArrayAdapter(this,android.R.layout.simple_list_item_1, nameList);
listview.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//Class creating Adapter
class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
public boolean hasStableIds() {
return true;
}
}
答案 0 :(得分:0)
我认为你有向后的HashMap。您希望它将列表视图位置(整数)映射到字符串。所以你想要HashMap&lt; Integer,String&gt ;;