0
我對Android比較陌生,使用在線API。我有兩個活動。第一項活動是調出搜索結果列表並將它們放入ListView中。這不是問題。我試圖從此ListView中選擇一個項目,並將其傳遞給下一個活動,並使用所選項目的信息填充此新活動中的另一個ListView。這是問題所在。第二項活動永不加載。獲得第二個活動的好方法是什麼?我一直在做研究,沒有得到預期的結果。下面是兩個活動和助手類的代碼。 謝謝Android - 多級列表視圖
的第一項活動
import helper.ArtistCalendarHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import services.XMLParser;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ArtistsSearchActivity extends ListActivity {
public static final String TAG = "ArtistsSearchActivity";
public static final String INTENT_EXTRAS_XML = "XML";
static final String KEY_ARTISTS = "artist";
static final String KEY_ID = "id";
static final String KEY_DISPLAY_NAME = "displayName";
static final String KEY_URI = "uri";
static final String KEY_ON_TOUR_UNTIL = "onTourUntil";
String onTourUntil ;
String displayName;
String identification;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artists_search);
String xml = getIntent().getStringExtra(HomeActivity.INTENT_EXTRAS_XML);
List<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
addArtistsToHashmap(menuItems, xml);
addItemsToList(menuItems);
}
private void addItemsToList(List<HashMap<String, String>> menuItems) {
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item_artist_search, new String[] { KEY_ID, KEY_DISPLAY_NAME,
KEY_URI, KEY_ON_TOUR_UNTIL }, new int[] { R.id.tvId,
R.id.tvDisplayName, R.id.tvUri, R.id.tvOnTourUntil });
setListAdapter(adapter);
}
private void addArtistsToHashmap(List<HashMap<String, String>> menuItems,
String xml) {
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_ARTISTS);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_ID, e.getAttribute(KEY_ID));
map.put(KEY_DISPLAY_NAME, e.getAttribute(KEY_DISPLAY_NAME));
map.put(KEY_URI, e.getAttribute(KEY_URI));
map.put(KEY_ON_TOUR_UNTIL, e.getAttribute(KEY_ON_TOUR_UNTIL));
menuItems.add(map);
}
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
displayName = ((TextView) view.findViewById(R.id.tvDisplayName)).getText().toString();
onTourUntil = ((TextView) view.findViewById(R.id.tvOnTourUntil)).getText().toString();
identification = ((TextView) view.findViewById(R.id.tvId)).getText().toString();
new AsyncDownload().execute(identification);
}
});
}
private class AsyncDownload extends AsyncTask<String, String, String> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ArtistsSearchActivity.this);
pDialog.setMessage("Please Wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
Log.v(TAG, "query is" + params[0]);
String result = new ArtistCalendarHelper().getXml(params[0]);
return result;
}
@Override
protected void onCancelled() {
super.onCancelled();
pDialog.dismiss();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
Log.v(TAG, "result=" + result);
Intent intent = new Intent();
intent.setClass(ArtistsSearchActivity.this, SingleArtistActivity.class);
intent.putExtra(KEY_DISPLAY_NAME, displayName);
intent.putExtra(KEY_ID, identification);
intent.putExtra(KEY_ON_TOUR_UNTIL, onTourUntil);
intent.putExtra(INTENT_EXTRAS_XML, result);
startActivity(intent);
}
}
}
第二個活動
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import services.XMLParser;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class SingleArtistActivity extends ListActivity {
public static final String TAG = "SingleArtistActivity";
public static final String INTENT_EXTRAS_XML = "XML";
static final String KEY_VENUE = "event";
static final String KEY_VENUE_NAME = "displayName";
static final String KEY_DISPLAY_NAME = "displayName";
static final String KEY_ON_TOUR_UNTIL = "onTourUntil";
static final String KEY_ID = "id";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_artist);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String displayName = in.getStringExtra(KEY_DISPLAY_NAME);
String onTourUntil = in.getStringExtra(KEY_ON_TOUR_UNTIL);
String id = in.getStringExtra(KEY_ID);
// Displaying all values on the screen
TextView lblDisplayName = (TextView) findViewById(R.id.display_name_label);
TextView lblOnTourUntil = (TextView) findViewById(R.id.on_tour_until_label);
TextView lblID = (TextView) findViewById(R.id.id_label);
lblDisplayName.setText(displayName);
lblOnTourUntil.setText(onTourUntil);
lblID.setText(id);
String xml = getIntent().getStringExtra(HomeActivity.INTENT_EXTRAS_XML);
List<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
addItemsToList(menuItems);
addCalendarToHashmap(menuItems, xml);
}
private void addItemsToList(List<HashMap<String, String>> menuItems) {
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item_artist_calendar, new String[] { KEY_VENUE_NAME }, new int[] { R.id.calendarDisplayName });
setListAdapter(adapter);
}
private void addCalendarToHashmap(List<HashMap<String, String>> menuItems,
String xml) {
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_VENUE);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_VENUE_NAME, e.getAttribute(KEY_VENUE_NAME));
menuItems.add(map);
}
}
}
而且我的助手類
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;
public class LocationCalendarHelper {
private static final String TAG = "LocationCalendarHelper";
private static final String SONGKICK_URL = "http://api.songkick.com/api/3.0/metro_areas/";
private static final String API_KEY = "yIekMi1hQzcFheKc";
public String getXml(String searchQuery) {
HttpClient httpclient = new DefaultHttpClient();
String getParameters = "";
try {
getParameters = URLEncoder.encode(searchQuery, "UTF-8")
+ "/calendar.xml?apikey=" + URLEncoder.encode(API_KEY, "UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String url = SONGKICK_URL + getParameters;
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
Log.i(TAG, response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
// now you have the string representation of the HTML request
instream.close();
return result;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
我的日誌貓錯誤。它從一堆xml開始,因此活動正在拉動信息,然後以錯誤結束
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <identifier mbid="eb558587-e7a1-4f2b-9fda-b6ff5e137ed1" href="http://api.songkick.com/api/3.0/artists/mbid:eb558587-e7a1-4f2b-9fda-b6ff5e137ed1.xml"/>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): </artist>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): </performance>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <performance billingIndex="3" displayName="Love And Death" billing="support" id="32720419">
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <artist displayName="Love And Death" uri="http://www.songkick.com/artists/286472-love-and-death?utm_source=19089&utm_medium=partner" id="286472">
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <identifier mbid="27796970-8f7a-4196-9f7d-87f091af4412" href="http://api.songkick.com/api/3.0/artists/mbid:27796970-8f7a-4196-9f7d-87f091af4412.xml"/>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <identifier mbid="c29b667e-9895-41d2-b5a5-1817050ce0bc" href="http://api.songkick.com/api/3.0/artists/mbid:c29b667e-9895-41d2-b5a5-1817050ce0bc.xml"/>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): </artist>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): </performance>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <venue displayName="Rostraver Ice Garden Arena" lat="40.1981235" lng="-79.8294916" uri="http://www.songkick.com/venues/51599-rostraver-ice-garden-arena?utm_source=19089&utm_medium=partner" id="51599">
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <metroArea displayName="Belle Vernon" uri="http://www.songkick.com/metro_areas/56020-us-belle-vernon?utm_source=19089&utm_medium=partner" id="56020">
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <state displayName="PA"/>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <country displayName="US"/>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): </metroArea>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): </venue>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <start time="19:30:00" datetime="2013-05-15T19:30:00-0500" date="2013-05-15"/>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <location lat="40.1981235" lng="-79.8294916" city="Belle Vernon, PA, US"/>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): </event>
05-10 07:09:41.587: V/ArtistsSearchActivity(796): <event type="Festival" status="ok" displayName="Rock on the Range 2013" uri="http://www.songkick.com/festivals/8261/id/14745014-rock-on-the-range-2013?utm_source=19089&utm_medium=partner" popularity="0
05-10 07:09:41.637: I/Choreographer(796): Skipped 49 frames! The application may be doing too much work on its main thread.
05-10 07:09:43.807: E/Trace(871): error opening trace file: No such file or directory (2)
05-10 07:09:45.357: E/Trace(910): error opening trace file: No such file or directory (2)
05-10 07:09:45.687: E/Trace(923): error opening trace file: No such file or directory (2)
05-10 07:09:46.307: E/Trace(936): error opening trace file: No such file or directory (2)
05-10 07:09:47.167: E/Trace(949): error opening trace file: No such file or directory (2)
05-10 07:09:48.337: E/Trace(975): error opening trace file: No such file or directory (2)
我有更新。上面的代碼工作!我會把它留給那些需要它的人。我遇到的問題是,如果xml字符串太長,搜索超時。那麼,我該如何讓Android有更多時間來進行搜索呢?
忘記調用adapter.notifyDatasetChange()。添加ItemsToList並調用addCalendarToHashmap(menuItems,xml);然後addItemsToList(menuItems);在onCreate() – 2013-05-10 05:39:26
謝謝,我收到一個錯誤,說明類型ListAdapter的notifyDatasetChange()未定義。有沒有應該使用的演員? – user2305700 2013-05-10 06:17:04
use adapter.notifyDataSetChanged(); – 2013-05-10 06:27:45