2014-10-16 72 views
-1

我解析從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(); 
        } 
       } 
    } 

} 

回答

2

利用這一點,並刪除所有其他的事情

play(feedItems.get(arg2).getVideoid()); 

編輯1:

(活動)getApplicationContext()

到位getApplicationContext使用這個()

+1

顯示異常java.lang.ClassCastException的屏幕截圖:io.bxbxbai.androiddemos.AppController不能投到android.app.Activity – AruLNadhaN 2014-10-16 12:12:01

+0

錯誤是由於getApplicationContext()將其更改爲此或使用VideoActivity.this – Meenal 2014-10-16 12:21:53

+0

仍然無法獲取該ID?我不知道爲什麼? – AruLNadhaN 2014-10-16 15:29:08

0
 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(feedItems.get(arg2).getVideoid()); 

      } 
     }); 
    } 
-1

如果feeditems是下面的代碼自定義對象類的使用,讓您的視頻ID

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

     feedItems feedItemslist= (feedItems) arg0.getAdapter().getItem(arg2); 
      mvideoid=feedItemslist.getmvideoid(); 
      System.out.println(mvideoid.toString()); 
      play(mvideoid); 


      } 
     }); 

:)

+0

您的TypeCast代碼無效 – SilentKiller 2014-10-16 11:47:02

+0

請具體說明您的答案,我上面寫道,您是否閱讀我發佈的答案的第一行 – koutuk 2014-10-16 12:11:10

+0

代碼不應該是假設。因爲你可以閱讀有問題的代碼。 – SilentKiller 2014-10-16 12:19:48

0

你是不是類型轉換你的對象。

嘗試下面的代碼

listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     // TypeCast your clicked Object 
     FeedItem item = (FeedItem) parent.getItemAtPosition(position); 
     play(item.getVideoid()); 
    } 
}); 
+0

您的TypeCast代碼無效 – SilentKiller 2014-10-16 11:48:27

+0

爲什麼不應該這樣?鑑於FeedItems是適配器中的項目,因此我在各種應用程序中使用類似的結構。 – Carsten 2014-10-16 11:50:38

+0

你確定AdapterView.getItemAtPosition(position)返回沒有TypeCasting適配器的「FeedItem」嗎? – SilentKiller 2014-10-16 11:51:57

0

這就是我的回答@SilentKiller