我有一个listView,其中填充了从YouTube获得的JSON数据:
private VideosListView listView;
截至目前 - listView已成功填充我正在寻找的数据,但我需要能够使用更新的数据重新填充它。目前 - 我使用:
执行/填充初始listViewnew GetYouTubeUserVideosTask(responseHandler, playlist).execute();
我想做的是使用String播放列表的不同值重新填充它:
String playlist = "TheMozARTGROUP";
new GetYouTubeUserVideosTask(responseHandler, playlist)
.execute();
View vg = findViewById(R.layout.home);
vg.invalidate();
P.S。
到目前为止我尝试使用的东西似乎没有用 - 当我滑动ViewPager时没有任何反应 - 但是如果我在那里设置吐司,它会在刷卡时出现。
我不知道为什么这是一个如此难以解决的问题 - 但我很乐意通过PayPal为任何可以解决这个问题的人留下小费。
http://i.stack.imgur.com/e0QqU.png
public class Home extends YouTubeBaseActivity implements
VideoClickListener {
private VideosListView listView;
private int mCurrentTabPosition = NO_CURRENT_POSITION;
private static final int NO_CURRENT_POSITION = -1;
private ListView drawerListView;
String playlist = "knocksteadyTV";
Activity activity;
int imageArray[];
String[] stringArray;
String runPrevious = "No";
private OnPageChangeListener mPageChangeListener;
ImagePagerAdapter adapter = new ImagePagerAdapter();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
final ViewPager mPager = (ViewPager) findViewById(R.id.view_pager);
adapter.notifyDataSetChanged();
mPager.setAdapter(adapter);
mPager.setOnPageChangeListener(mPageChangeListener);
listView = (VideosListView) findViewById(R.id.videosListView);
listView.setOnVideoClickListener(this);
new GetYouTubeUserVideosTask(responseHandler, playlist).execute();
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int pos) {
Toast.makeText(getApplicationContext(), "onPageSelected",
Toast.LENGTH_SHORT).show();
String playlist = "TheMozARTGROUP";
new GetYouTubeUserVideosTask(responseHandler, playlist).execute();
adapter.notifyDataSetChanged();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(), "onPageScrolled",
// Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrollStateChanged(int pos) {
Toast.makeText(getApplicationContext(),
"onPageScrollStateChanged", Toast.LENGTH_SHORT).show();
String playlist = "TheMozARTGROUP";
new GetYouTubeUserVideosTask(responseHandler, playlist)
.execute();
adapter.notifyDataSetChanged();
}
});
mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
position = mCurrentTabPosition;
int oldPos = mPager.getCurrentItem();
if (position > oldPos) {
System.out.print(position);
// Moving to the right
String playlist = "TheMozARTGROUP";
new GetYouTubeUserVideosTask(responseHandler, playlist)
.execute();
View vg = findViewById(R.layout.home);
vg.invalidate();
} else if (position < oldPos) {
// Moving to the Left
System.out.print(position);
String playlist = "TheMozARTGROUP";
new GetYouTubeUserVideosTask(responseHandler, playlist)
.execute();
View vg = findViewById(R.layout.home);
vg.invalidate();
}
mPager.setOnPageChangeListener(mPageChangeListener);
}
private void onTabChanged(PagerAdapter adapter,
int mCurrentTabPosition, int position) {
// TODO Auto-generated method stub
}
};}
private class MyActivityGetYouTubeUserVideosTask extends GetYouTubeUserVideosTask {
public MyActivityGetYouTubeUserVideosTask(Handler replyTo,
String username) {
super(replyTo, username);
// TODO Auto-generated constructor stub
}
@Override
protected void onPostExecute(Void result) {
if (result != null) {
// TODO update your list data
// note: assuming your data is stored in an ArrayList<MyData>,
// you cannot create a new one. you must clear() the current
// ArrayList and add() the result to it.
}
adapter.notifyDataSetChanged();
}
}
private void _initMenu() {
}
Handler replyTo = new Handler() {
@Override
public void handleMessage(Message msg) {
populateListWithVideos(msg);
};
};
Handler responseHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
populateListWithVideos(msg);
};
};
private void populateListWithVideos(Message msg) {
Library lib = (Library) msg.getData().get(
GetYouTubeUserVideosTask.LIBRARY);
listView.setVideos(lib.getVideos());
}
@Override
protected void onStop() {
responseHandler = null;
super.onStop();
}
@Override
public void onVideoClicked(Video video) {
Intent intent = new Intent(this, Player.class);
intent.putExtra("id", video.getId());
intent.putExtra("title", video.getTitle());
intent.putExtra("uploader", video.getUploader());
intent.putExtra("viewCount", video.getviewCount());
startActivity(intent);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class ImagePagerAdapter extends PagerAdapter {
public ImagePagerAdapter(Activity act, int[] mImages,
String[] stringArra) {
imageArray = mImages;
activity = act;
stringArray = stringArra;
}
// this is your constructor
public ImagePagerAdapter() {
super();
}
private int[] mImages = new int[] { R.drawable.selstation_up_btn,
R.drawable.classical_up_btn, R.drawable.country_up_btn,
R.drawable.dance_up_btn, R.drawable.hiphop_up_btn,
R.drawable.island_up_btn, R.drawable.latin_up_btn,
R.drawable.pop_up_btn, R.drawable.samba_up_btn };
private String[] stringArray = new String[] { "vevo",
"TheMozARTGROUP", "TimMcGrawVEVO", "TiestoVEVO",
"EminemVEVO" };
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Home.this;
ImageView imageView = new ImageView(context);
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
drawerListView.setItemChecked(position, true);
setTitle("......");
String text = "menu click... should be implemented";
Toast.makeText(Home.this, text, Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(drawerListView);
}
}
}
}
public class GetYouTubeUserVideosTask extends AsyncTask<Void, Void, Void> {
public static final String LIBRARY = "Library";
private final Handler replyTo;
private final String username;
public GetYouTubeUserVideosTask(Handler replyTo, String username) {
this.replyTo = replyTo;
this.username = username;
}
/*
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Void doInBackground(Void... arg0) {
try {
HttpClient client = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?author="+username+"&v=2&alt=jsonc");
HttpResponse response = client.execute(request);
String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
List<Video> videos = new ArrayList<Video>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String title = jsonObject.getString("title");
String id = jsonObject.getString("id");
String viewCount = jsonObject.getString("viewCount");
String uploader = jsonObject.getString("uploader");
String url;
try {
url = jsonObject.getJSONObject("player").getString("mobile");
} catch (JSONException ignore) {
url = jsonObject.getJSONObject("player").getString("default");
}
String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("hqDefault");
videos.add(new Video(title, id, viewCount, uploader, url, thumbUrl));
}
Library lib = new Library(username, videos);
Bundle data = new Bundle();
data.putSerializable(LIBRARY, lib);
Message msg = Message.obtain();
msg.setData(data);
replyTo.sendMessage(msg);
} catch (ClientProtocolException e) {
Log.e("Feck", e);
} catch (IOException e) {
Log.e("Feck", e);
} catch (JSONException e) {
Log.e("Feck", e);
}
return null;
}
/*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Void result) {
//adapter.notifyDataSetChanged();
}
}