我正在写3个类/活动,我希望(最终)能够使用stock android layouts将解析后的XML数据加载到simpleListAdapter中。
我的策略是活动A:启动SAX解析器,加载处理程序,检索ArrayList>来自Activity C,并将ArrayList加载到simpleListAdapter中。
活动B是处理程序(我知道它有效,因为我之前使用过它,没有问题)。
最后,Activity C包含返回Strings的方法,这些方法由处理程序给定值,然后将这些字符串加载到ArrayList>通过意图发送到活动A.
但所有出现的都是黑屏。
我正在使用的测试XML网站是http://www.google.com/ig/api?weather=Boston+Massachusetts“
这是Activity A + C的代码(我知道处理程序有效)。
活性(A):
public class xmlparser extends ListActivity implements OnClickListener {
static final String XMLsite = "http://www.google.com/ig/api weather=Boston+Massachusetts";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@SuppressWarnings("unchecked")
@Override
try{
URL website = new URL(XMLsite);
//getting xmlreader to parse data
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
HandlingXMLStuff doingWork = new HandlingXMLStuff();
xr.setContentHandler(doingWork);
xr.parse(new InputSource(website.openStream()));
ArrayList<HashMap<String, String>> list =(ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}catch(Exception e){
}
}
活性(C):
package com.school;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class XMLDataCollected extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, xmlparser.class);
ArrayList<HashMap<String, String>> list = buildData();
intent.putExtra("arraylist", list);
startActivity(intent);
}
int temp = 0;
String city = null;
public void setCity(String c){
city = c;
}
public void setTemp(int t){
temp = t;
}
private ArrayList<HashMap<String, String>> buildData() {
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
list.add(putData(city, "temp"));
list.add(putData("city", "temp"));
list.add(putData("city", "temp"));
return list;
}
private HashMap<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;
}
}
任何建议都会有所帮助。
P.S。我之前使用过这个XML过程将数据加载到textview中,所以我知道它能够至少解析XML标签并将它们设置在Activity C中的方法上,然后在textview中检索和设置字符串,但是我的最终目标是能够将信息加载到simpleListAdapter中。
由于
答案 0 :(得分:1)
正如其他人所说,你的代码很悲惨。此操作不需要两个活动。而是考虑使用XMLParser停留在Activity 1中的AsyncTask或Service。然后,负责的Activity是启动AsyncTask或Service并在数据可用时检索(解析)它。
public class ActivityOne extends Activity {
private static final String ADDRESS = "...";
@Override public void onCreate(Bundle icicle) {
super.onCreate(icicle);
new FetchDataTask(ADDRESS).execute();
}
private class FetchDataTask extends AsyncTask<Void, Void, Void> {
private URL mWebsite;
private ArrayList<???> mListActivityContent;
public FetchDataTask(String address) {
mWebsite = new URL(address);
}
@Override public void doInBackground(Void... voids) {
//getting xmlreader to parse data
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
HandlingXMLStuff doingWork = new HandlingXMLStuff();
xr.setContentHandler(doingWork);
xr.parse(new InputSource(mWebsite.openStream()));
/*
* Now, you are going to want other parsing and
* array building stuff here. Looking at your code,
* I can't figure out how the XMLDataCollected
* Activity is used, sorry, so you will have to tweak
* this to accomodate it.
*/
}
@Override public void onPostExecute(Void voids) {
SimpleAdapter adapter = new SimpleAdapter(ActivityOne.this,
list, android.R.layout.simple_list_item_2, from, to);
ActivityOne.this.setListAdatper(adapter);
}
}
}
答案 1 :(得分:1)
以下是使用simpleAdapter将国家/地区名称和国家/地区代码从java local列入listview的示例示例 SimpleAdapter详细了解@ http://www.tutorialsbuzz.com/2014/06/simpleadaper-bind-hashmap-listview.html
另一个示例解析XML并使用SimpleAdapter @ http://www.tutorialsbuzz.com/2015/03/android-sax-xml-parser-http.html
显示到ListView中listview项目的XML布局
file:list_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:textSize="18dp" />
<TextView
android:id="@+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" />
</LinearLayout>
file:MainActivity.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
ArrayList<HashMap<String, String>> countries;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] locales = Locale.getISOCountries();
countries = new ArrayList<HashMap<String, String>>();
for (String countryCode : locales) {
Locale obj = new Locale("", countryCode);
HashMap<String, String> country = new HashMap<String, String>();
String country_name = obj.getDisplayCountry();
String country_code = obj.getCountry();
country.put("name", country_name);
country.put("code", country_code);
countries.add(country);
}
// keys of hashmap
String[] from = { "name", "code" };
// view id's to which data to be binded
int[] to = { R.id.name_id, R.id.code_id };
//Creating Adapter
ListAdapter adapter = new SimpleAdapter(MainActivity.this, countries,
R.layout.list_items, from, to);
//Setting Adapter to ListView
setListAdapter(adapter);
}
}