Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnItemClickListener

Tags:

android

I have an error that says "OnItemClickListener cannot be resolved to a type" when I enter this code in:

package com.funkystudios.android.facts;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;






public class activity2 extends ListActivity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   String[] Facts = getResources().getStringArray(R.array.Facts_Array);
   setListAdapter(new ArrayAdapter<String>(this, R.layout.list, Facts));
   ListView lv = getListView();
   lv.setTextFilterEnabled(true);
   lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        // When clicked, show a toast with the TextView text
        Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
            Toast.LENGTH_SHORT).show();
      }
    });

 }
}

It occurs right at the "lv.setOnItemClickListener(new OnItemClickListener() {". I'm not sure what I'm doing wrong.

like image 466
Keenan Thompson Avatar asked Oct 14 '10 02:10

Keenan Thompson


2 Answers

I figured it out, I had imported the wrong items!

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener;

That is what it should look like.

like image 147
Keenan Thompson Avatar answered Nov 16 '22 14:11

Keenan Thompson


I have an error that says"The method setOnTouchListener(View.OnTouchListener) in the type View is not applicable for the arguments (new CarouselAdapter.OnItemClickListener(){})"

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Carousel carousel = (Carousel)findViewById(R.id.carousel);
    carousel.setOnItemClickListener(new OnItemClickListener(){
        LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout01);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        //String[]img = getResources().getStringArray(R.array.entries);

        //img.setDrawingCacheEnabled(true);
        //img.setOnTouchListener(this);
        //@Override
        public void onItemClick(CarouselAdapter<?> parent, View view,
                int position, long id) {                
            Toast.makeText(MainActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show();               
            view.setDrawingCacheEnabled(true);
            view.setOnTouchListener(this);
        }

    });
like image 20
user1029593 Avatar answered Nov 16 '22 14:11

user1029593