-2

I am having a problem with my setOnClickListener. I can not figure out what the code is i need for it. What i am trying to do is once the item is clicked on in the list view it opens up a new activity. in my code the list view is in the MainActivity. and i want it to open up the Homework activity. So my question is, can anybody help me figure out what i need to put in for it to work correctly and open up Homework.java? when it opens up Homework.java it would show the item clicked in the list view as the header. then nothing in the body.

MainActivity.class:

public class VideoListTask extends AsyncTask<Void, Void, Void>{
ProgressDialog dialog;
protected void onPreExecute (Void result) {
    dialog.getProgress();
    super.onPostExecute(result);
}
@Override
   protected  Void doInBackground(Void... params)
{
    HttpClient client = new DefaultHttpClient();
    //HttpGet getRequest = new HttpGet(feedUrl);
    Date now = new Date();

    HttpGet getRequest = new HttpGet(canvasUrl + "courses?      include[]=term&state=available");

    getRequest.setHeader("Authorization","Bearer " + canvasApiKey); //uses your key to access your data
    try
    {
       HttpResponse response = client.execute(getRequest);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if(statusCode != 200)
        {
            return null;
        }
        InputStream jsonStream = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(jsonStream));
        StringBuilder builder = new StringBuilder();
        String line;
        while((line = reader.readLine())!=null)
        {
            builder.append(line);
        }
        String jsonData = builder.toString();
        //JSONObject json = new JSONObject(jsonData);
        //JSONObject data = json.getJSONObject("data");
        //JSONArray items = data.getJSONArray("items");
        JSONArray courses = new JSONArray(jsonData);
        //for(int i =0; i<items.length(); i++)
        //{
        //    JSONObject video = items.getJSONObject(i);
        //    videoArrayList.add(video.getString("title"));
        //}

        for(int i = 0; i<courses.length(); i++)
        {
            JSONObject course = courses.getJSONObject(i);
            JSONObject term = course.getJSONObject("term");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            try {
              Date enddate = format.parse(term.getString("end_at"));
              Date startdate = format.parse(term.getString("start_at"));
              if (now.after(startdate) && now.before(enddate))
              {
                  videoArrayList.add(course.getString("name"));
              }
            } catch (Exception e) {
              //videoArrayList.add(course.getString("name"));//include if you want      undated courses
            }

        }
    }
    catch (ClientProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;


   THIS IS WHERE I NEED TO PUT THE ONCLICK LISTENER IN. 
     }
1011
  • 121
  • 1
  • 15
  • Go to my this answer:[http://stackoverflow.com/questions/22010883/asynctask-android-executionexception-error/22011013?noredirect=1#comment33364178_22011013](http://stackoverflow.com/questions/22010883/asynctask-android-executionexception-error/22011013?noredirect=1#comment33364178_22011013) – M D Feb 25 '14 at 16:37
  • can you try helping me out with my code? What do you recommend that i personally do? – user3271688 Feb 25 '14 at 16:40

2 Answers2

0

If Homework.java is your second activity you can set a click listener in this way

Main Activity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    ListView myListView = (ListView) findViewById(R.id.myListView);
    myListView.setOnItemClickListener(new OnItemClickListener()
    {
       @Override
       public void onItemClick(AdapterView<?> adapter, View v, int position,
             long arg3) 
       {
              startActivity(new Intent(MainActivity.this, Homework.class));
       }
    });
simoneL
  • 602
  • 1
  • 7
  • 23
0
start.setOnClickListener(new View.OnClickListener() {   
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                    try{
                    Class<?> ourClass=Class.forName("com.example.projname.Homework");
                    Intent ourIntent= new Intent(MainActivity.this,ourClass);
                    ourIntent.putExtra("matrix", m);
                    startActivity(ourIntent);
                    }catch(ClassNotFoundException e){
                        e.printStackTrace();

                    }
             });

The data you pass using putExtra will be available to you in the Homeactivity.java

ben_joseph
  • 1,660
  • 1
  • 19
  • 24