Tomino
Tomino

Reputation: 113

Can't use getResources() within CursorAdapter subclass

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 ()
{

}
}

Upvotes: 0

Views: 275

Answers (4)

Dilroop Singh
Dilroop Singh

Reputation: 544

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()

Upvotes: 0

Neha Shukla
Neha Shukla

Reputation: 3660

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().

Upvotes: 0

M D
M D

Reputation: 47817

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.

Upvotes: 1

Hariharan
Hariharan

Reputation: 24853

Try this..

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

    case 0:
        den = context.getResources().getString(R.string.pondelok);
        break;

Upvotes: 0

Related Questions