2016-02-12 217 views
1

返回錯誤的數據你好我使用avocarrot SDK原生廣告。我已經在我的列表視圖&中成功實現了廣告展示效果非常好。但是當我點擊listview使用setOnClickListener我得到錯誤的數據。下面是我的代碼..列表視圖setOnClickListener從列表視圖

avocarrot setlistadapter

avocarrotInstream = new com.avocarrot.androidsdk.AvocarrotInstream(
      listAdapter,  /* pass your listAdapter */ 
      this,     /* reference to your Activity */ 
      "my api key",  /* replace with your Avocarrot API Key */ 
      "my placement key" /* replace with your Avocarrot Placement Key */ 
    ); 
    avocarrotInstream.setLogger(true, "ALL"); 
    avocarrotInstream.setSandbox(true); 

    avocarrotInstream.setLayout(R.layout.avocarrot_feed_row, R.id.avo_container, R.id.feed_title,   R.id.feed_description, R.id.feed_icon, R.id.feed_image, R.id.feed_button); 

    // Bind the created avocarrotInstream adapter to your list instead of your listAdapter 
    listView.setAdapter(avocarrotInstream); 

我onclicklistener

private class Click2 implements ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) { 

     Object obj = avocarrotInstream.getItem(position); 
     String name = ((TextView) view.findViewById(R.id.name)).getText() 
       .toString(); 
     String status = ((TextView) view.findViewById(R.id.txtStatusMsg)) 
       .getText().toString(); 

     String bitmap = ((FeedItem) feedItems.get(position)).getImge(); 
     Intent in = new Intent(NewBlogStyleHindi.this, SingleActivity.class); 
     in.setType("text/html"); 
     in.putExtra(TAG_TITLE, name); 
     in.putExtra("images", bitmap); 
     in.putExtra(TAG_TEXT, status); 
     startActivity(in); 
    } 
} 

和avocarrot只要列表視圖setonclicklistener作爲

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
     /* get your object from avocarrotInstream for clicked position */ 
     Object obj = avocarrotInstream.getItem(position); 
    } 
    }); 

由於廣告排在增加..

+0

() );'? –

+0

那是我在用,但由於原生廣告排增加,所以我得到的數據一個列表項back..if有在整個飼料數據的兩個廣告,然後我回去使用setOnClickListener 2個列表項數據,你能告訴我如何訪問列表視圖數據使用此行 Object obj = avocarrotInstream.getItem(position); –

+0

該行有錯誤嗎?我不明白爲什麼這是行不通的。 –

回答

3

這是Avocarrot支持團隊的Nikos。

我想你已經與我們的支持服務聯繫我們,但我張貼在這裏的答案,以防萬一別人有類似的問題。

在你OnItemClickListener,你必須使用對象從Avocarrot適配器,而不是直接從您的ArrayList訪問對象。

所以你的情況:

Object obj = avocarrotInstream.getItem(position); 

將在正確的位置返回FeedItem和OnItemListener將變爲:你爲什麼不使用`listView.setOnItemClickListener(新CLICK2

private class Click2 implements ListView.OnItemClickListener { 
@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    FeedItem feed = (FeedItem) avocarrotInstream.getItem(position); 
    String name = feed.getName(); 
    String status = feed.getStatus(); 
    String bitmap = feed.getImge(); 
    Intent in = new Intent(NewBlogStyleHindi.this, SingleActivity.class); 
    in.setType("text/html"); 
    in.putExtra(TAG_TITLE, name); 
    in.putExtra("images", bitmap); 
    in.putExtra(TAG_TEXT, status); 
    startActivity(in); 

    } 
}