Sunday, April 21, 2013

[android help] Android - How to work with nested ListViews


this is my first question on this site and I am also new to Android. I am creating an application using an online API. I am working with this API in XML and parsing the responses into ListViews. I have reached a point where I would like to select an item from a ListView in one activity and send that information to the next activity along with another ListView containing more information for the selected item. As an example, one activity has a list of bands. Clicking on the band name will bring up the band name and a list of tour dates on the next activity. According to my API, the band's ID number is needed to access the bands tour information I am trying to pass the ID number as a search parameter but cannot get this to work. I did manage to find a decent tutorial on androidhive.info but cannot seem to be able to apply these techniques. The doInBackground() method is where my app is hanging up.



ListView lv = getListView();

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
// getting values from selected ListItem
String displayName = ((TextView) view.findViewById(R.id.tvDisplayName)).getText().toString();
String onTourUntil = ((TextView) view.findViewById(R.id.tvOnTourUntil)).getText().toString();
String identification = ((TextView) view.findViewById(R.id.tvId)).getText().toString();

// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleArtistActivity.class);
in.putExtra(KEY_DISPLAY_NAME, displayName);
in.putExtra(KEY_ID, identification);
in.putExtra(KEY_ON_TOUR_UNTIL, onTourUntil);

new AsyncDownload().execute(identification);

startActivity(in);
}
});


}



private class AsyncDownload extends AsyncTask {

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;
}


My AsyncDownload class is called in my onClickListener. The class calls a helper that contains the URL and API key.



public class ArtistCalendarHelper {
private static final String TAG = "ArtistCalendarHelper";
private static final String SONGKICK_URL = "http://api.songkick.com/api/3.0/artists/";
private static final String API_KEY = "yIekMi1hQzcFheKc";

public String getXml(String identification) {

HttpClient httpclient = new DefaultHttpClient();

String getParameters = "";
try {
getParameters = URLEncoder.encode(identification, "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;


These methods worked for obtaining the bands name in an initial search. How could this be changed to perform a search for tour information with an argument retrieved from a ListView? Is this different from getting a search query from an EditText field? I didn't think there would be much of a difference. I have tried to include the affected code. I am not sure how much code I should provide.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...