无法将字符串值传递给onItemClick?

时间:2014-10-16 11:39:15

标签: android json listview onitemclicklistener

我正在解析json中的值并创建自定义列表视图,如http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/

我正在通过json传递videoid并且选择了listitem。我希望将videoid传递给play方法。 我怎么能这样做? 在此先感谢!

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

}

5 个答案:

答案 0 :(得分:2)

在onItemclick中使用它并删除所有其他内容

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

编辑1:

(Activity)getApplicationContext()

使用它代替getApplicationContext()

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

             }
       });
  }

答案 2 :(得分:0)

你不是TypeCasting你的对象。

尝试以下代码

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

答案 3 :(得分:0)

这是我的答案@SilentKiller

的屏幕截图

答案 4 :(得分:-1)

如果Feeditems是您的自定义对象类,请使用下面的代码来获取您的videoid

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


            }
        });

:)