getresources in adapter android

https:‮www//‬.lautturi.com
getresources in adapter android

In an Android app, you can use the getResources() method of the Context class to get a Resources object, which provides access to a variety of resources such as strings, colors, and dimensions.

Here's an example of how you could use the getResources() method to get a string resource in an ArrayAdapter:

import android.content.Context;
import android.widget.ArrayAdapter;

public class MyAdapter extends ArrayAdapter<String> {
  private final Context context;

  public MyAdapter(Context context, String[] values) {
    super(context, android.R.layout.simple_list_item_1, values);
    this.context = context;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    TextView textView = (TextView) super.getView(position, convertView, parent);
    String string = context.getResources().getString(R.string.my_string);
    textView.setText(string);
    return textView;
  }
}

In this example, the getResources() method is used to get a Resources object from the Context object passed to the adapter's constructor. The getString() method of the Resources class is then used to get the my_string string resource, which is set as the text of the TextView.

You can also use the getResources() method to get other types of resources, such as colors or dimensions. For example:

int color = context.getResources().getColor(R.color.my_color);
float dimension = context.getResources().getDimension(R.dimen.my_dimension);

In these examples, the getColor() and getDimension() methods are used to get the my_color color resource and the my_dimension dimension resource, respectively.

Created Time:2017-11-01 12:05:01  Author:lautturi