Question

newsitem is a class that hold my data. when I click an item in the list view I get this error:

01-19 11:03:20.094: E/AndroidRuntime(860): FATAL EXCEPTION: main

01-19 11:03:20.094: E/AndroidRuntime(860): java.lang.ClassCastException: java.lang.Integer cannot be cast to com.example.NewsItem

01-19 11:03:20.094: E/AndroidRuntime(860):  at com.example.ListViewContents$2.onItemClick(ListViewContents.java:72)

01-19 11:03:20.094: E/AndroidRuntime(860):  at android.widget.AdapterView.performItemClick(AdapterView.java:282)

01-19 11:03:20.094: E/AndroidRuntime(860):  at android.widget.AbsListView.performItemClick(AbsListView.java:1037)

01-19 11:03:20.094: E/AndroidRuntime(860):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2449)

01-19 11:03:20.094: E/AndroidRuntime(860):  at android.widget.AbsListView.onTouchEvent(AbsListView.java:3085)

01-19 11:03:20.094: E/AndroidRuntime(860):  at android.widget.ListView.onTouchEvent(ListView.java:3591)

when clicked should be open an activity.

this is onCreate method in the my ListviewContents class:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listviewcontents);
    intent = getIntent();
    url_topic = intent.getIntExtra("key", 0);
    listview = (ListView)findViewById(R.id.custom_list);
    adapter = new LazyImageLoadAdapter(this,arraylist_newitems);
    listview.setAdapter(adapter);

    Button buttonrefresh = (Button)findViewById(R.id.button_refresh);
    buttonrefresh.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            adapter.imageLoader.clearCache();
            adapter.notifyDataSetChanged();
        }
    });

    new LongImageFetch().execute((Void)null);

     listview.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
                String url ;
                NewsItem newitem = (NewsItem) listview.getItemAtPosition(position);
                url = newitem.getUrllink();
                Intent intent = new Intent(getApplicationContext(),WebViewActivity.class);
                intent.putExtra("URL", url);
                startActivity(intent);
              }
            });

}
Was it helpful?

Solution

java.lang.ClassCastException: java.lang.Integer cannot be cast to com.example.NewsItem

listview.getItemAtPosition(position); returns a Integer value.

But you have

NewsItem newitem = (NewsItem) listview.getItemAtPosition(position);
// casting it to  NewsItem 

You probably want

NewsItem newitem = (NewsItem)arraylist_newitems.get(position)

OTHER TIPS

The OnItemClickListener passes you the view to use as a parameter.

Does this work:

NewsItem newitem = (NewsItem)view;

?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top