我解析從json的值,並創建一個像這樣的自定義列表視圖http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/無法將字符串值傳遞給onItemClick?
我通過JSON傳遞一個videoid,當選擇listitem時。我想把videoid傳遞給play方法。 我該怎麼做? 在此先感謝!在onItemclick
public class VideoActivity extends Activity {
private static final String TAG = "Mine";
private static final int REQ_START_STANDALONE_PLAYER = 1;
private static final int REQ_RESOLVE_SERVICE_MISSING = 2;
public static final String DEVELOPER_KEY = "AIzaSyDcnoqJGI1KEVSZIMg872s";
public ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
public String mvideoid;
private String URL_FEED = "http://arulnadhan.hol.es/vid.json";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_list);
listView = (ListView) findViewById(R.id.feed_list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
// making fresh volley request and getting json
GsonRequest<FeedResult> gsonRequest = new GsonRequest<FeedResult>(URL_FEED, FeedResult.class,
new Response.Listener<FeedResult>() {
@Override
public void onResponse(FeedResult response) {
feedItems = response.getFeedItems();
listAdapter.setData(feedItems);
listAdapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addRequest(gsonRequest, TAG);
getid();
}
/**
* Parsing json reponse and passing the data to list adapter
*/
private void parseJsonFeed(JSONObject response) {
try {
JSONArray feedArray = response.getJSONArray("feed");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
FeedItem item = new FeedItem();
item.setId(feedObj.getInt("id"));
String name = feedObj.isNull("name") ? null : feedObj
.getString("name");
item.setName(name);
// Image might be null sometimes
String image = feedObj.isNull("image") ? null : feedObj
.getString("image");
item.setImageUrl(image);
item.setStatus(feedObj.getString("status"));
// videoid might be null sometimes
mvideoid = feedObj.isNull("videoid") ? null : feedObj
.getString("videoid");
item.setVideoid(mvideoid);
feedItems.add(item);
}
// notify data changes to list adapater
listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getid(){
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d(TAG, Integer.toString(arg2));
Log.d(TAG, mvideoid);
play(mvideoid);
}
});
}
public void play(String mvideoid){
int startIndex = 0;
int startTimeMillis = 0;
boolean autoplay = true;
boolean lightboxMode = false;
Toast.makeText(VideoActivity.this, this.mvideoid, Toast.LENGTH_LONG).show();
Intent intent = null;
intent = YouTubeStandalonePlayer.createPlaylistIntent((Activity) getApplicationContext(), DEVELOPER_KEY,
mvideoid, startIndex, startTimeMillis, autoplay, lightboxMode);
startActivity(intent);
if (intent != null) {
if (canResolveIntent(intent)) {
startActivityForResult(intent, REQ_START_STANDALONE_PLAYER);
} else {
// Could not resolve the intent - must need to install or update the YouTube API service.
YouTubeInitializationResult.SERVICE_MISSING
.getErrorDialog((Activity) getApplicationContext(), REQ_RESOLVE_SERVICE_MISSING).show();
}
}
}
}
顯示異常java.lang.ClassCastException的屏幕截圖:io.bxbxbai.androiddemos.AppController不能投到android.app.Activity – AruLNadhaN 2014-10-16 12:12:01
錯誤是由於getApplicationContext()將其更改爲此或使用VideoActivity.this – Meenal 2014-10-16 12:21:53
仍然無法獲取該ID?我不知道爲什麼? – AruLNadhaN 2014-10-16 15:29:08