SetOnCLickListener问题

时间:2014-02-25 16:33:55

标签: java android

我的setOnClickListener出了问题。我无法弄清楚我需要的代码是什么。我想要做的是,一旦在列表视图中单击该项,它就会打开一个新活动。在我的代码中,列表视图位于MainActivity中。我想让它打开家庭作业活动。所以我的问题是,任何人都可以帮我弄清楚我需要投入什么才能让它正常工作并打开Homework.java?当它打开Homework.java时,它会显示在列表视图中单击的项目作为标题。然后身体里什么都没有。

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

2 个答案:

答案 0 :(得分:0)

如果Homework.java是您的第二个活动,您可以用这种方式设置点击监听器

主要活动

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

答案 1 :(得分: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();

                    }
             });

使用putExtra传递的数据将在Homeactivity.java

中可用