3

getIntent does not get recognized, what I am doing wrong?

I get this error:

error: cannot find symbol variable getIntent

PS: at first I got this error cannot find symbol method getIntent.

My code:

package com.example.r.app;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;


public class MyAdapter extends ArrayAdapter<String> {

    int i=0;

    public MyAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView textView = (TextView) view.findViewById(android.R.id.text1);

        Intent intent = getIntent;
        int[] barva = intent.getIntArrayExtra("barva");

        int[] bar = {1, 2, 3};

        if (position == i) {
            if (barva == bar){
                textView.setTextColor(0xffcc80);
            }
            else if (barva == bar){
                textView.setTextColor(0xa5d6a7);
            }
            else if (barva == bar){
                textView.setTextColor(0x80deea);
            }

        }
         return view;
    }

}
Everlastinf
  • 33
  • 1
  • 1
  • 5

3 Answers3

4

getIntent is a method. Inside an activity try:

Intent intent = getIntent();

Edit:

I didnt really noticed you're inside an Adapter. If your context relates to the activity, you have to do it slightly different:

Intent intent = ((Activity) context).getIntent();
3

error: cannot find symbol variable getIntent

First,getIntent is method instead of variable so call it as:

Intent intent = getIntent();

Second, getIntent() method is from Activity class instead of ArrayAdapter class so this method is not available in class which is extending ArrayAdapter.

Call getIntent() method from Activity in which creating object of MyAdapter and pass intent as parameter to constructor.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Ok, I've did it, but now I get this error: non-static method getIntent() cannot be referenced from static context. What am I doing wrong? – Everlastinf Feb 26 '15 at 18:13
  • You mean this? Intent intent = Results.getIntent(); int[] barva = intent.getIntArrayExtra("barva"); – Everlastinf Feb 26 '15 at 18:31
  • This is where I call it from, but it is not in onCreate..: Intent intent3 = new Intent(Game.this, MyAdapter.class); int [] barva2 = Arrays.copyOf(barva, g); intent3.putExtra("barva", barva2); startActivityForResult(intent3, 1); – Everlastinf Feb 26 '15 at 18:34
  • How could then I pass array int to MyAdapter? – Everlastinf Feb 26 '15 at 18:38
  • @Everlastinf: show code where adding `MyAdapter` adapter to ListView – ρяσѕρєя K Feb 26 '15 at 18:39
  • `Intent intent = getIntent(); String[] rez = intent.getStringArrayExtra("key"); List rez3 = Arrays.asList(rez); MyAdapter adapter = new MyAdapter(getListView().getContext(), R.layout.row, rez3); getListView().setAdapter(adapter);` – Everlastinf Feb 26 '15 at 18:41
  • @Everlastinf: fixed or not? – ρяσѕρєя K Feb 26 '15 at 19:00
  • It did, but it doesn't seem to pass any value. – Everlastinf Feb 26 '15 at 19:04
  • I think something is wrong with if statements.. because all text in textView is transperant..http://i57.tinypic.com/qx85xe.png if you are willing to check this out. – Everlastinf Feb 26 '15 at 19:17
  • It is the same.. trasperant text. – Everlastinf Feb 26 '15 at 19:24
  • I did and still no text. http://i61.tinypic.com/2rh0i1t.png here is code of activity where listView is called. – Everlastinf Feb 26 '15 at 19:32
  • @Everlastinf : see following tutorial and change getView method probably help http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter – ρяσѕρєя K Feb 26 '15 at 19:38
  • It was something wrong with setTextColor.. now it works, because I have used different code (example: textView.setTextColor(Color.parseColor("#a5d6a7"));) Thank for everything! – Everlastinf Feb 26 '15 at 20:10
0

Or you can write in your class declaration:

public class nameofyourclass extends AppCompatActivity {