PullToRefreshListView上的ClassCashException

时间:2014-10-20 15:06:47

标签: android-listview pull-to-refresh

我正在尝试使用来自eu.erikw.PullToRefreshListView项目的ListView来实现PullToRefresh。我使用自定义适配器来填充我的listview。但是在onItemClick事件中,我收到此错误java.lang.ClassCastException:android.widget.HeaderViewListAdapter无法强制转换为com.example.xxapp.RssAdapter,其中我的RssAdapter类是

public class RssAdapter extends BaseAdapter{

private final List<RssItem> items;
private final Context context;

public RssAdapter(Context context, List<RssItem> items) {
    this.items = items;
    this.context = context;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

@Override
public long getItemId(int id) {
    return id;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = View.inflate(context, R.layout.rss_item, null);
        holder = new ViewHolder();
        holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.itemTitle.setText(items.get(position).getTitle());
}

static class ViewHolder {
    TextView itemTitle;
}
}

和RssItem类是

public class RssItem {

private final String title;
private final String link;

public RssItem(String title, String link) {
    this.title = title;
    this.link = link;
}

public String getTitle() {
    return title;
}

public String getLink() {
    return link;
}
}

这是给出错误的onclick方法

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Log.e("Clicked item", "Position is " +position);

    //line below gives ClasscastException error
    RssAdapter adapter = (RssAdapter) parent.getAdapter();

    RssItem item = (RssItem) adapter.getItem(position);
    Uri uri = Uri.parse(item.getLink());

    Intent i = new Intent(getActivity(),WebViewActivity.class);
    i.putExtra("mystring",uri.toString());
    startActivity(i);
}

我需要有关onclick方法的帮助,以便我可以获得显示网页的链接。

2 个答案:

答案 0 :(得分:0)

我找到了办法。在我将适配器设置为listview之前,我创建了另一个与RssItem具有相同形式的listarray,因此我可以将适配器分配给此listarray。稍后当我需要这个适配器时,我只需要调用新的listarray,在这种情况下不会受到我添加到listview的标头的影响。感谢njzk2关于将我的适配器保持为实例成员的评论。解决方案如下所示:

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Log.e("Clicked item", "Position is " +position);
    TextView title = (TextView) view.findViewById(R.id.itemTitle);
    String titleItem = title.getText().toString();

    Log.e("Title", titleItem);

    //RssAdapter adapter = (RssAdapter) parent.getAdapter();

    //RssItem item = (RssItem) adapter.getItem(position);
    RssItem item = itemx.get(position);
    Uri uri = Uri.parse(item.getLink());

    Intent i = new Intent(getActivity(),WebViewActivity.class);
    i.putExtra("mystring",uri.toString());
    startActivity(i);
}

其中itemx是我的适配器的实例。我不知道这样做有多有效但是有效

答案 1 :(得分:0)

你应该使用那个

HeaderViewListAdapter hlva = (HeaderViewListAdapter)parent.getAdapter();
RssAdapter adapter = (RssAdapter)hlva.getWrappedAdapter();