Question

I want to replace one string with another string drawn from the shared preference:

public class DisplayAdapter extends BaseAdapter {
    private final static String MY_PREFERENCES = "MyPref";
    private final static String GESTIONE = "Gestione";
    private Context mContext;
    private ArrayList<String> id;
    private ArrayList<String> gestioneName;


    public DisplayAdapter(Context c, ArrayList<String> gestione,ArrayList<String> id) {
        this.mContext = c;

        this.id = id;
        this.gestioneName = gestione;

    }

    public int getCount() {
        // TODO Auto-generated method stub
        return id.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int pos, View child, ViewGroup parent) {
        SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
        String Gestione = prefs.getString(GESTIONE, "Entrata");
        Holder mHolder;
        LayoutInflater layoutInflater;
        if (child == null) {
            layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            child = layoutInflater.inflate(R.layout.row, null);
            mHolder = new Holder();
            mHolder.txt_gestione = (TextView) child.findViewById(R.id.txt_gestione);
            mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);

I would like to replace

String Gestione = "Entrata";

replacing it with this:

SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
String Gestione = prefs.getString(GESTIONE, "Entrata");

In dispay adapter if I put the string of data that comes from the shared preference class gives me a fairy error to the line of the string, how can I fix it?

Was it helpful?

Solution

Make sure you declare Context in Adapter's Constructor.

private Context context

public CustomAdapter(Context ctx...) {
    this.context = ctx;
    ...
}

public View getView(int post, View child, ViewGroup parent) {
    SharedPreferences prefs = context.getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
    String Gestione = prefs.getString(GESTIONE, "Entrata");
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top