我想从ListView
填充ArrayList
。
ArrayList
填充了正确的值。在模拟器上,我得到的是具有值
packagename ClassName @ someNumber
有人有同样的问题吗?
public class ExchangeMoneyMKActivity extends Activity {
Document dom;
private static final String METHOD_NAME = "GetExchangeRate";
private static final String NAMESPACE = "http://www.nbrm.mk/klservice/";
private static final String SOAP_ACTION=NAMESPACE+METHOD_NAME;
private static final String URL = "http://www.nbrm.mk/klservice/kurs.asmx?kurs";
ListView lw;
ArrayList<String>currencyShortNames=new ArrayList<String>();
ArrayList<String>currencyRates=new ArrayList<String>();
ArrayList<ExchangeRate> currencyList=new ArrayList<ExchangeRate>();
ArrayAdapter<ExchangeRate> aa;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
currencyShortNames.add("EUR");
currencyShortNames.add("USD");
currencyShortNames.add("GBP");
currencyShortNames.add("CHF");
currencyShortNames.add("SEK");
currencyShortNames.add("NOK");
currencyShortNames.add("JPY");
currencyShortNames.add("DKK");
currencyShortNames.add("CAD");
currencyShortNames.add("AUD");
lw=(ListView)this.findViewById(R.id.listView1);
aa = new ArrayAdapter<ExchangeRate>(
this,android.R.layout.simple_list_item_1,
currencyList);
lw.setAdapter(aa);
callService();
}
private void callService() {
try{
SoapObject request=new SoapObject(NAMESPACE,METHOD_NAME);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
java.util.Date date = new java.util.Date();
request.addProperty("StartDate",dateFormat.format(date));
request.addProperty("EndDate",dateFormat.format(date));
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//MyXmlParserHandler parser=new MyXmlParserHandler();
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
String resultData = result.toString();
// System.out.println(resultData);
int strStart=resultData.lastIndexOf("schema");
int strStop=resultData.lastIndexOf("dsKurs");
int strLength=strStop-strStart-10;
String responseXML=resultData.substring(strStart+10,strLength);
responseXML.replace("<", "<");
responseXML.replace(">", ">");
String xmlDocument="<?xml version=\"1.0\" encoding=\"utf-8\" ?>"+
"<dsKurs>" +
responseXML +
"</dsKurs>";
System.out.println(xmlDocument);
XMLfromString(resultData);
}catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void XMLfromString(String resultData) throws ParserConfigurationException, SAXException, IOException {
// TODO Auto-generated method stub
InputStream is = new ByteArrayInputStream(resultData.getBytes("UTF-8"));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
try{
dom = db.parse(is);
}catch(Exception e){
System.out.println("Fatal error");
}
dom.getDocumentElement();
NodeList nl = dom.getElementsByTagName("KursZbir");
currencyList.clear();
if (nl != null && nl.getLength() > 0) {
for (int i = 0 ; i < nl.getLength(); i++) {
Element kursZbir = (Element)nl.item(i);
Element sreden = (Element)kursZbir.getElementsByTagName("Sreden").item(0);
currencyRates.add(sreden.getFirstChild().getNodeValue());
}
}
for(int i=0;i<currencyShortNames.size();i++){
currencyList.add(new ExchangeRate(currencyShortNames.get(i).toString(),currencyRates.get(i).toString()));
System.out.println(currencyList.get(i).shName.toString());
System.out.println(currencyList.get(i).Currency.toString());
aa.notifyDataSetChanged();
}
}
}
答案 0 :(得分:2)
您实际上需要一个自定义ArrayAdapter for ExchangeRate。目前,您的列表视图不知道要显示的ExchangeRate的哪个属性,并且可能在每个汇率实例上调用.toString()
class ExchangeRateAdapter extends ArrayAdapter<ExchangeRate> {
public ExchangeRateAdapter(Context context, ArrayList<ExchangeRate> rate) {
super(context, android.R.layout.simple_spinner_item, rate);
}
public View getView(int position, View convertView, ViewGroup parent)
{
ExchangeRate rate = (ExchangeRate) getItem(position);
TextView view = new TextView(this.getContext());
view.setTextColor(0);
view.setHeight(30);
view.setText(rate.getAmount());
return view;
}
}
然后
aa = new ExchangeRateAdapter();
list.setAdapter(aa);
在getView(..)方法中,代码专门告诉ListView显示Amount而不再显示.toString()
答案 1 :(得分:0)
ListView myListView = (ListView) findViewById(R.id.myListView);
listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
myArrayList);
myListView.setAdapter(listAdapter);
答案 2 :(得分:0)
没有任何东西可以做到这一点。但是,我创建了自己的ListAdapater版本,它实现了java.util.List,并允许您将java.util.List的任何实现设置到其中。基本上它的工作原理如下:
public class abstract ListAdapter<T> extends BaseAdapter implements List<T> {
List<T> list;
public ListAdapter( List<T> contents ) {
list = contents;
}
public int getCount() {
return list.size();
}
public T getItem(int i) {
return list.get(i);
}
public long getItemId( int i ) {
return i;
}
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
public boolean contains( Object o ) {
return list.contains(o);
}
public boolean addAll( Collection<? extends T> c ) {
boolean result = list.addAll(c);
notifyDataSetChanged();
return result;
}
// so on and so on essentially delegating every method in List
// interface to the underlying list. Of course mutators will have to call
// notifyDataSetChanged()
}
我还有其他一些改进,使用规定的视图回收机制更容易创建和绑定到视图。因此,您实现了createView()和bindView(),而不是实现getView()。
最后,它使创建灵活列表变得轻而易举:
public void onCreate( Bundle saved ) {
ListAdapter<String> adapter = new ListAdapater<String>() { ... };
adapter.add( "Foo" );
adapter.add( "Bar" );
adapter.add( "BaZ" );
listView.setAdapter( adapter );
}
它比开箱即用的Android产品更具生产力。