我是开发 Android 应用程序的新手。我想要做的是将数据存储到数组中并将该数据显示到列表活动视图中。在某种程度上,该应用程序将类似于解析树,单击列表中的一个项目将带您进入另一个项目,依此类推。问题是我将有多个不同的列表,并且大多数都会以某种方式连接(取决于事先选择的类别)。我一直在研究 MapsDemo 示例应用程序并且知道有一种方法 - 但是我还没有弄清楚。如果这令人困惑,请告诉我......
谢谢大家
我是开发 Android 应用程序的新手。我想要做的是将数据存储到数组中并将该数据显示到列表活动视图中。在某种程度上,该应用程序将类似于解析树,单击列表中的一个项目将带您进入另一个项目,依此类推。问题是我将有多个不同的列表,并且大多数都会以某种方式连接(取决于事先选择的类别)。我一直在研究 MapsDemo 示例应用程序并且知道有一种方法 - 但是我还没有弄清楚。如果这令人困惑,请告诉我......
谢谢大家
开始尝试这个,然后一旦你可以做到这一点,然后转向使用动态数据。我将它用于一个非常相似的项目并且效果很好:
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
这是我使用的代码。也许这会帮助你:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.dining);
//Hard coded array
ListData newListItem = new ListData();
ArrayList<ListData> itemObject=null;
try {
itemObject= newListItem.getData(DATAURL);
Log.e(TAG, "item object: "+itemObject.toString());
ListAdapter adapter = new ListAdapter(this, R.layout.dininglistview, itemObject);
//Create listview
this.listView1 = (ListView)this.findViewById(android.R.id.list);
this.listView1.setAdapter(adapter);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Unexpected error", e);
} catch (ServiceException e){
AlertDialog alertDialog = new AlertDialog.Builder(DiningActivity.this).create();
alertDialog.setTitle("Temporarily unavailable");
alertDialog.setMessage("Please contact top25@uievolution.com");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent homeIntent = new Intent(DiningActivity.this, HomeActivity.class);
DiningActivity.this.startActivity(homeIntent);
}
});
alertDialog.show();
}
//implement dining adapter
}
这是我用来解析 XML 的 getData() 方法:
public ArrayList<ListData> getData(String DATAURL) throws InterruptedException, ServiceException {
ArrayList<ListData> items=null;
HttpURLConnection conn = null;
try {
URL url = new URL(DATAURL);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(TIMEOUT);
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
if(Thread.interrupted())
{
throw new InterruptedException();
}
items = parseXML(conn);
} catch (MalformedURLException e) {
Log.e(TAG, "Invalid URL", e);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Unable to connect to URL", e);
}
finally{
if(conn!=null){
conn.disconnect();
}
}
return items;
}
这是我的解析器:
private static ArrayList<ListData> parseXML(HttpURLConnection conn) throws ServiceException {
ArrayList<ListData> dataArray = new ArrayList<ListData>();
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
Log.e(TAG, "Parse Configuration issue", e);
throw new ServiceException("Service Exception Error");
} catch (IllegalAccessError e){
Log.e(TAG, "Illegal Accessor Error", e);
throw new ServiceException("Service Exception Error");
}
try {
//parse input from server
Document document = builder.parse(conn.getInputStream());
Element xmlElement = document.getDocumentElement();
NodeList recordNodes = xmlElement.getChildNodes();
//assign parsed data to listItem object
for(int i=0; i<recordNodes.getLength(); i++){
Node record = recordNodes.item(i);
NodeList recordDetails = record.getChildNodes();
ListData listItem = new ListData();
for(int ii=0; ii<recordDetails.getLength(); ii++){
Node detailItem = recordDetails.item(ii);
String detailType = detailItem.getNodeName();
String detailValue = detailItem.getTextContent();
//assign attributes to listItem object
if(detailType.matches("item")){
int itemValue = Integer.parseInt(detailValue);
listItem.setItem(itemValue);
} else if(detailType.matches("title")){
listItem.setTitle(detailValue);
} else if(detailType.matches("subhead")){
listItem.setSubhead(detailValue);
} else if(detailType.matches("thumb")){
ImageManager im = new ImageManager();
Bitmap tImg = im.getImage(detailValue);
listItem.setThumb(tImg);
} else if(detailType.matches("photo")){
ImageManager im = new ImageManager();
Bitmap pImg = im.getImage(detailValue);
listItem.setPhoto(pImg);
} else if(detailType.matches("localAddress")){
listItem.setLocalAddress(detailValue);
} else if(detailType.matches("phone")){
listItem.setPhone(detailValue);
} else if(detailType.matches("webUrl")){
URL webUrl1 = new URL(detailValue);
listItem.setWebURL(webUrl1);
} else if(detailType.matches("facebook")){
listItem.setFacebook(detailValue);
} else if(detailType.matches("twitter")){
listItem.setTwitter(detailValue);
} else if(detailType.matches("latitude")){
float itemLat = Float.parseFloat(detailValue);
listItem.setLatitude(itemLat);
} else if(detailType.matches("longitude")){
float itemLon = Float.parseFloat(detailValue);
listItem.setLatitude(itemLon);
} else if(detailType.matches("notes")){
listItem.setNotes(detailValue);
} else if(detailType.matches("comments")){
listItem.setComments(detailValue);
}
}
dataArray.add(listItem);
}
} catch (SAXException e) {
//TODO
e.printStackTrace();
} catch (IOException e) {
//TODO
e.printStackTrace();
}
return dataArray;
}