0

I've been working on a social app on android and I have an issue that I couldn't solve. I have a button in every item of my ListView.

When the user presses this button, the background changes to another color but when I close the app the color of the button should become the default color.

davejal
  • 6,009
  • 10
  • 39
  • 82
Frank
  • 71
  • 2
  • 10
  • 2
    I hope you are creating model for the `listview`. You can store them in `SharedPreferences` using `Set`. See [this](http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) for reference. – Faraz Jan 18 '16 at 17:49
  • What do you mean by a "model for the listview" ? – Frank Jan 18 '16 at 17:54
  • Okay... I will post a sample code to explain you better. – Faraz Jan 18 '16 at 17:56
  • Thank you, I appreciate it . – Frank Jan 18 '16 at 19:06
  • Hi Frank, sorry for late reply. I found using `GSON` is much simpler than `Set`. I have posted the answer, let me know if it works. – Faraz Jan 19 '16 at 18:16
  • Please add what you tried yourself, so the actual (or existing) code can actually be reviewed. – davejal Jan 20 '16 at 18:04

2 Answers2

0

Try this:

Create a model class for storing the color information of each list item.

ListModel.java

public class ListModel implements Serializable
{
    String title;
    int color;

    public ListModel(String title, int color) {
        this.title = title;
        this.color = color;
    }

    public String getTitle() {
        return title;
    }

    public int getColor() {
        return color;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setColor(int color) {
        this.color = color;
    }
}

In activity, create initialize the ListView and assign an adapter to it. Here, when user presses the back button, we will convert the list of color information into JSON using GSON library and store it into SharedPreferences. And when activity is created, get the JSON from the SharedPreferences and again convert it back into List object using GSON.

Activity.java

public class MainActivity extends AppCompatActivity {

    private MyListAdapter adapter;
    private List<ListModel> dataList;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        preferences = PreferenceManager.getDefaultSharedPreferences(this);

        if (preferences.contains("Key")) {
            String jsonStr = preferences.getString("Key", "");
            Type type = new TypeToken<List<ListModel>>() {
            }.getType();
            dataList = new Gson().fromJson(jsonStr, type);
        } else {
            dataList = new ArrayList<>();
            fillData();
        }

        ListView listView = (ListView) findViewById(R.id.listview);

        adapter = new MyListAdapter(this, dataList);
        listView.setAdapter(adapter);
    }

    public void changeColor(int position) {
        dataList.get(position).setColor(getResources().getColor(R.color.blue));
    }

    private void fillData() {
        int color = getResources().getColor(R.color.black);
        dataList.add(new ListModel("Button 1", color));
        dataList.add(new ListModel("Button 2", color));
        dataList.add(new ListModel("Button 3", color));
        dataList.add(new ListModel("Button 4", color));
        dataList.add(new ListModel("Button 5", color));
        dataList.add(new ListModel("Button 6", color));

        adapter.notifyDataSetChanged();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        SharedPreferences.Editor editor = preferences.edit();
        //Set the values
        Gson gson = new Gson();
        String json = gson.toJson(dataList);
        editor.putString("Key", json);
        editor.apply();
    }
}

MyListAdapter.java

public class MyListAdapter extends BaseAdapter
{
    private final Context context;
    private final List<ListModel> list;

    public MyListAdapter(Context context, List<ListModel> list)
    {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }
    /*private view holder class*/
    private class ViewHolder {
        Button button;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.button = (Button) convertView.findViewById(R.id.listItemBtn);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }


        ListModel model = list.get(position);
        holder.button.setText(model.getTitle());
        holder.button.setBackgroundColor(model.getColor());
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.setBackgroundColor(context.getResources().getColor(R.color.blue));
                ((MainActivity)context).changeColor(position);

            }
        });
        return convertView;
    }
}

And in build.gradle, add GSON dependency

compile 'com.google.code.gson:gson:2.2.4'
Faraz
  • 2,144
  • 1
  • 18
  • 28
0

Essentially you shouldn't rely on the the buttons themselves, you should have something called model behind the scene to store state of those buttons. So, you can save that model, which stores state of all buttons, to a persistent memory (e.g. Database, file, Shared Preferences, etc) when your app is closed.

frogatto
  • 28,539
  • 11
  • 83
  • 129