关闭应用程序后如何保存按钮的背景?

时间:2016-01-18 17:44:38

标签: android

我一直在研究Android上的社交应用程序,我有一个我无法解决的问题。我的ListView的每个项目都有一个按钮。

当用户按下此按钮时,背景会变为另一种颜色,但当我关闭应用程序时,按钮的颜色应该成为默认颜色。

2 个答案:

答案 0 :(得分:0)

试试这个:

创建一个模型类,用于存储每个列表项的颜色信息。

<强> 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;
    }
}

在活动中,创建初始化ListView并为其分配适配器。在这里,当用户按下后退按钮时,我们会使用JSON库将颜色信息列表转换为GSON并将其存储到SharedPreferences中。创建活动后,从JSON获取SharedPreferences,然后使用List再次将其转换回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;
    }
}

build.gradle中,添加GSON依赖

compile 'com.google.code.gson:gson:2.2.4'

答案 1 :(得分:0)

基本上你不应该依赖按钮本身,你应该在场景后面有一个叫做 model 的东西来存储那些按钮的状态。因此,当您的应用关闭时,您可以将存储所有按钮状态的模型保存到持久性内存(例如数据库,文件,共享首选项等)。