1. Are you ready for the Galaxy S20? Here is everything we know so far!

cannon add a launch intent to an onItemclick in async task

Discussion in 'Android Development' started by searayman, Jul 2, 2013.

  1. searayman

    searayman Lurker
    Thread Starter

    Basically I have an async task that runs to populate a listView. When I try and set the onitemclick for the listView to launch a new activity I can not:


    [HIGH]public class GetBreweryStatisticsJSON extends AsyncTask
    <String, Void, String> {

    Context c;

    public GetBreweryStatisticsJSON(Context context)
    {
    c = context;
    }

    @Override
    protected String doInBackground(String... arg0) {
    // TODO Auto-generated method stub
    return readJSONFeed(arg0[0]);
    }

    protected void onPostExecute(String result){
    //decode json here
    try{
    JSONArray jsonArray = new JSONArray(result);


    //acces listview
    ListView lv = (ListView) ((Activity) c).findViewById(R.id.yourBreweryStatistics);

    //make array list for beer
    final List<BreweryInfo> tasteList = new ArrayList<BreweryInfo>();



    for(int i = 0; i < jsonArray.length(); i++) {

    String brewery = jsonArray.getJSONObject(i).getString("brewery");
    String rate = jsonArray.getJSONObject(i).getString("rate");
    String breweryID = jsonArray.getJSONObject(i).getString("id");

    int count = i + 1;

    brewery = count + ". " + brewery;

    Log.d("brewery stats" , brewery);

    //create object
    BreweryInfo tempTaste = new BreweryInfo(brewery, breweryID, rate);

    //add to arraylist
    tasteList.add(tempTaste);


    //add items to listview
    BreweryInfoAdapter adapter1 = new BreweryInfoAdapter(c ,R.layout.brewer_stats_listview, tasteList);
    lv.setAdapter(adapter1);

    //set up clicks
    lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
    int arg2, long arg3) {
    BreweryInfo o=(BreweryInfo)arg0.getItemAtPosition(arg2);

    String bID = o.breweryID;

    Intent i = new Intent(c, BreweryPage2.class);
    i.putExtra("beerID", bID);
    startActivity(i);



    }
    });

    }

    }
    catch(Exception e){

    }

    }

    public String readJSONFeed(String URL) {
    StringBuilder stringBuilder = new StringBuilder();
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(URL);
    try {
    HttpResponse response = httpClient.execute(httpGet);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
    HttpEntity entity = response.getEntity();
    InputStream inputStream = entity.getContent();
    BufferedReader reader = new BufferedReader(
    new InputStreamReader(inputStream));
    String line;
    while ((line = reader.readLine()) != null) {
    stringBuilder.append(line);
    }
    inputStream.close();
    } else {
    Log.d("JSON", "Failed to download file");
    }
    } catch (Exception e) {
    Log.d("readJSONFeed", e.getLocalizedMessage());
    }
    return stringBuilder.toString();
    }



    }[/HIGH]


    It keeps saying:

    The method startActivity(Intent) is undefined for the type new AdapterView.OnItemClickListener(){}
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...