Tomino
Tomino

Reputation: 113

Android, intent error "not defined"

My Eclipse gives an error message with intents in switch: The constructor Intent (new Adapter.View.OnItemClickListener(){}, Class <UlohaShowerActivity>) is undefined

All project intents are OK, but this can get a reason. I don't know what is wrong with this statement. I am using exactly these intents from other activities and it works great, activities are in manifest and ... only in this switch and OnItemClick is there the error.

Here's my code for the activity,

public class HladajListActivity extends Activity 
{
private DatabaseOp mDbHelper;
public static final String HOW = "how";
public static final String NAME = "name";
public static final String DATE = "date";
public static final String TYP = "typ";

int how;
String name;
String date;
int typ;

String username;
String nazov;
String datum;

ListView listview;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hladaj);

    Intent i = getIntent();
    how = Integer.parseInt(i.getStringExtra(HOW));
    name = i.getStringExtra(NAME);
    date = i.getStringExtra(DATE);
    typ = Integer.parseInt(i.getStringExtra(TYP));

    listview = (ListView) findViewById(R.id.listHladaj);

    mDbHelper = new DatabaseOp(this);
    mDbHelper.open();

    showUserSettings(); 
    Cursor hladajCursor = mDbHelper.fetchAllFind(username, name, date, how, typ);

    final HladajCursorAdapter adapter = new HladajCursorAdapter(this, hladajCursor);
    listview.setAdapter(adapter);

    if (listview.getCount()==0)
        setContentView(R.layout.hladaj_empty);

    listview.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int stlpec, long arg3) 
        {
            // TODO Auto-generated method stub
            Cursor cur = (Cursor) adapter.getItem(stlpec);

            String typ = cur.getString(cur.getColumnIndex("typ"));
            String odosli = cur.getString(cur.getColumnIndex("_id"));

            switch (Integer.parseInt(typ)) 
            {
                case 0:
                    Intent intent = new Intent(this,UlohaShowerActivity.class);
                    intent.putExtra(UlohaShowerActivity.ODOSLI, odosli);

                    startActivity(intent);
                    break;
                case 1:
                    Intent intent = new Intent(this,PredmetShowerActivity.class);
                    intent.putExtra(PredmetShowerActivity.ODOSLI, odosli);

                    startActivity(intent);
                    break;
                case 2:
                    Intent intent = new Intent(this,PoznamkaShowerActivity.class);
                    intent.putExtra(PoznamkaShowerActivity.ODOSLI, odosli);

                    startActivity(intent);
                    break;
            }
        }
    });
}

@Override
public void onPause() 
{
    super.onPause();
    mDbHelper.close();
}


private void showUserSettings() 
{
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    username = sharedPrefs.getString("prefUsername", "NULL");
}

}

Upvotes: 0

Views: 445

Answers (3)

M D
M D

Reputation: 47817

You should change this

Intent intent = new Intent(this,UlohaShowerActivity.class);

To

Intent intent = new Intent(HladajListActivity.this,UlohaShowerActivity.class);

and also change for all other cases. You have to pass Activity Context as first Argument of Intent.

Upvotes: 1

tianwei
tianwei

Reputation: 1879

Intent intent = new Intent(this,UlohaShowerActivity.class);

TO

 Intent intent = new Intent(HladajListActivity.this,UlohaShowerActivity.class);

And

Intent intent = new Intent(this,PoznamkaShowerActivity.class);

TO

Intent intent = new Intent(HladajListActivity.this,PoznamkaShowerActivity.class);

Your [this] is just the instance of OnItemClickListener not of Activity.

Upvotes: 0

laalto
laalto

Reputation: 152817

Replace

new Intent(this

with

new Intent(HladajListActivity.this

this inside the OnItemClickListener refers to the OnItemClickListener instance and not the outer activity instance. You need to explicitly refer to the activity which is a Context.

Upvotes: 0

Related Questions