listview setOnClickListener从listview返回错误的数据

时间:2016-02-12 06:32:09

标签: android listview

您好我使用avocarrot sdk作为原生广告。我已成功实施了我的listview&广告展示得非常好。但是当我使用setOnClickListener点击listview listitem时,我得到了错误的数据。下面是我的代码..

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提供listview 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);
    }
  });

由于广告排的增加..

1 个答案:

答案 0 :(得分:3)

这是来自Avocarrot支持团队的Nikos。

我认为您已经通过我们的支持服务与我们联系,但我在这里发布了一个答案,以防其他人遇到类似的问题。

在OnItemClickListener中,您必须使用Avocarrot Adapter中的对象,而不是直接从ArrayList访问您的对象。

所以在你的情况下:

Object obj = avocarrotInstream.getItem(position);

将FeedItem返回到正确的位置,OnItemListener将更改为:

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

    }
}