1

我的 ViewPager 中有 7 个面板。每个都有不同的功能。对于我的新闻面板,我希望在视图中具有功能。这是我目前使用的 ViewPager 类:

class MyPageAdapter extends PagerAdapter {

  public int getCount() {
    //Return 7 as there will be 7 panes in the dashboard
    return 7;
  }

  public Object instantiateItem(View collection, int position) {

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = null;
    switch (position) {
    case 0:
        v = inflater.inflate(R.layout.dashboard_social, null);
        break;
    case 1:
        v = inflater.inflate(R.layout.dashboard_unilife, null);
        break;
    case 2:
        v = inflater.inflate(R.layout.dashboard_allcourses, null);
        break;
    case 3:
        v = inflater.inflate(R.layout.dashboard_news, null);
        //USE HTTP HERE FOR TWITTER FEED


        break;
    case 4:
        v = inflater.inflate(R.layout.dashboard_mycourse, null);
        break;
    case 5:
        v = inflater.inflate(R.layout.dashboard_media, null);
        break;
    case 6:
        v = inflater.inflate(R.layout.dashboard_extras, null);
        break;
    }

    ((ViewPager) collection).addView(v, 0);

    return v;
  }

  @Override
  public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);

  }

  @Override
  public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);

  }

   @Override
   public Parcelable saveState() {
    return null;
   }
 }

我知道对于一个简单的功能,比如使用一个按钮,你会有这样的东西:

case 3:

        v = inflater.inflate(R.layout.dashboard_news, null);
    //Functionality     
        Button btn = (Button) v.findViewById(R.id.bTutorialComplete);
        final Context context = collection.getContext();
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context,
                        DashboardContainerActivity.class));
            }
        });

我的问题是我有一个单独的活动,可以从 Twitter 检索最新的推文并显示它。这是显示最新推文的代码:

public class DashboardNews extends Activity{

TextView tvFeed;
HttpClient client;
//Incomplete URL of Twitter timeline
final static String URL = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
//Used in AsyncTask to carry out lastTweet method
JSONObject json;



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.twitter_feed);
    tvFeed = (TextView) findViewById(R.id.tvTwitterFeedText);
    new Read().execute("text");
}

public JSONObject lastTweet(String username) throws ClientProtocolException, IOException, JSONException{
    //Used to create a string that can be appended
    StringBuilder url = new StringBuilder(URL);
    //append the username to the url
    url.append(username);

    //new HttpGet
    HttpGet get = new HttpGet(url.toString());

    //new response
    HttpResponse r = client.execute(get);

    //get status code
    int status = r.getStatusLine().getStatusCode();
    //check if status code is success (200)
    if(status == 200){
        //Get the entity from response
        HttpEntity e = r.getEntity();

        //Convert entity data to string
        String data = EntityUtils.toString(e);

        //Get JSON formatted sequence of values
        JSONArray timeline = new JSONArray(data);

        //Place the sequence in a JSONObject
        JSONObject lasttweet = timeline.getJSONObject(0);

        //return the last tweet
        return lasttweet;
    } else {
        //Display a Toast that there was an error
        Toast.makeText(getBaseContext(), "Error connecting to Twitter.", Toast.LENGTH_SHORT).show();
        //Return null
        return null;
    }
}

public class Read extends AsyncTask<String, Integer, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
            //Call lastTweet method and username
            json = lastTweet("kibria3");
            //return the last tweet
            return json.getString(params[0]);


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //Set the text of view to the result
        tvFeed.setText(result);
    }



}

}

我的问题是如何让这个 Activity 在 View Pager 的 switch 语句的案例 3 中工作?(用 HTTP 连接替换按钮功能)

4

0 回答 0