0

please why is my Eclipse writing error with getResources() ?? It is writing: The method getResources() is undefined for the type PredmetCursorAdapter

What is wrong with it ?? On all lines with this metode i have that error, but in other classes is all okay

public class PredmetCursorAdapter extends CursorAdapter {

public PredmetCursorAdapter(Context context, Cursor c) {
    super(context, c);
    // TODO Auto-generated constructor stub
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // TODO Auto-generated method stub
    TextView textViewUlohaName = (TextView) view.findViewById(R.id.nazovPredmetu);
    textViewUlohaName.setText(cursor.getString(cursor.getColumnIndex("nazov")));

    String den;
    int dlzka = Integer.parseInt(cursor.getString(cursor.getColumnIndex("dlzka")));

    switch (dlzka)  
    {
        case 0:
            den = getResources().getString(R.string.pondelok);
            break;
        case 1:
            den = getResources().getString(R.string.utorok);
            break;
        case 2:
            den = getResources().getString(R.string.streda);
            break;
        case 3:
            den = getResources().getString(R.string.stvrtok);
            break;
        case 4:
            den = getResources().getString(R.string.piatok);
            break;
    }

    TextView textViewUlohaDate = (TextView) view.findViewById(R.id.denPredmetu);
    textViewUlohaDate.setText(den);

    // upravit hodnotu podla [od - do]
    TextView textViewUlohaTime = (TextView) view.findViewById(R.id.casPredmetu);
    textViewUlohaTime.setText(cursor.getString(cursor.getColumnIndex("hodina")));

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.predmet_simple_list_item, parent,
            false);

    return retView;
}

public void dni ()
{

}
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Tomino
  • 113
  • 2
  • 14
  • 1
    try `context.getResources().getString(R.string.pondelok);` instead – M D Apr 13 '14 at 13:48
  • Have a look at the use of Context [here](http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context/13945633#13945633). – ChuongPham Apr 13 '14 at 13:53

4 Answers4

1

You have to access resources using Context in your PredmetCursorAdapter like below:

 switch (dlzka)  
{
    case 0:
        den = context.getResources().getString(R.string.pondelok);
        break;
    case 1:
        den = context.getResources().getString(R.string.utorok);
        break;
    case 2:
        den = context.getResources().getString(R.string.streda);
        break;
    case 3:
        den = context.getResources().getString(R.string.stvrtok);
        break;
    case 4:
        den = context.getResources().getString(R.string.piatok);
        break;
}

For more information go to this nice SO POST.

Community
  • 1
  • 1
M D
  • 47,665
  • 9
  • 93
  • 114
0

Try this..

Use context.getResources() do like that remaining all case

    case 0:
        den = context.getResources().getString(R.string.pondelok);
        break;
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

If you are trying to get resource from activity then you can call

getResource() 

directly. But if you are trying to use getResource() from any class then you can call this on the object of Context, which you can pass from activity to class where you want to call getResource().

Like if Context is mContext then use

mContext.getResource().
Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
0

Context is necessary to add before getResources() because context informs with which activity's reference you want to get Resources .

same is used in many case even when we inflate() a layout , we need to pass reference. context.getMenuInflater().inflate(R.menu.main, menu);

so you should use: context.getResources()

Dilroop Singh
  • 544
  • 6
  • 16