如何在创建新活动时记住单个ListView项的颜色

时间:2014-09-09 22:46:23

标签: android listview listitem

这是我更改单个ListView项目颜色的方法。但我想记住上一个选定项目的颜色,如果Activity已创建(在onCreate()方法中),则显示它。

public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);


        l.getChildAt(position).setBackgroundColor(Color.GREEN);

        if (save != -1 && save != position){
            l.getChildAt(save).setBackgroundColor(Color.WHITE);
        }

        save = position;                
}

修改

这是onCreate方法:

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_go_to_alarams);


            preferences = this.getSharedPreferences("com.example.projektzaliczeniowy", Context.MODE_PRIVATE);
            lv = (ListView) findViewById(android.R.id.list);

            setListAdapter(adapter);

            int save = preferences.getInt("positionNr", -1);

            if (save != -1){
                lv.getChildAt(save).setBackgroundColor(Color.GREEN); // **HERE IS AN ERROR** 

            }
    }

这是我xml的片段:

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.39"
        android:clickable="true" >
</ListView>

对不起我的英文:)你能帮帮我吗?

5 个答案:

答案 0 :(得分:1)

将if的代码更改为:

    SharedPreferences prefs = this.getSharedPreferences(getPackageName(),MODE_PRIVATE);
    int save = prefs.getInt("selected", -1);
    if (save != -1 && save != position){
        l.getChildAt(save).setBackgroundColor(Color.WHITE);
    }
    prefs.edit().putInt("selected",position).apply();

在你的onCreate上

SharedPreferences prefs = this.getSharedPreferences(getPackageName(),MODE_PRIVATE);
int save = prefs.getInt("selected", -1);
if(save!=-1)
    listview.getChildAt(save).setBackgroundColor(Color.WHITE);

答案 1 :(得分:1)

您可以在SharedPreferences

中存储所选项目的颜色和位置

编辑:由于SharedPreference已经作为@Pedro Oliveira的回答,我会给你一些其他选择。

班级的全局变量:

int position;
int color;
ListView lView;

您可以将位置存储在Bundle中,即savedInstanceState

public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // This bundle will be passed to onCreate if the process is
    // killed or reset.
    savedInstanceState.putInt("Position", position);
    savedInstanceState.putInt("Color", color);
}

然后在onCreate()

public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // here you can initialize your ListView    
    lView = (ListView) findViewById(YOUR_ID);

    // get the position and color from the instance
    // state we saved in onSaveInstanceState
    if(savedInstanceState != null) {
        position = savedInstanceState.getInt("Position");
        color = savedInstanceState.getInt("Color");
    }

    // Create OnListItemClickListener and color the item at position

}

Bundle将传递给onRestoreInstanceState(),因此您也可以使用它:

public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // This bundle has also been passed to onCreate.
    position = savedInstanceState.getInt("Position");
    color = savedInstanceState.getInt("Color");
}

要记住列表项,您还可以为每个列表项指定一个ID:

  • 通过调用setId(__ID__)方法
  • 动态生成
  • 在您的XML文件中静态添加ListView属性
  • 定义id="__ID__"

然后,您可以将此ID字符串存储在SharedPreferencesBundle savedInstanceState中,并使用此ID来获取onCreate()中的项目。

答案 2 :(得分:1)

使用共享偏好

存储int颜色值

SharedPreferences pref = getActivity().getSharedPreferences("preferences_app", 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt("color", Color.GREEN);
        editor.commit();

//检索并使用int颜色值

SharedPreferences pref = getActivity().getSharedPreferences("preferences_app", 0);
    SharedPreferences.Editor editor = pref.edit();
    Integer color = pref.getInt("color", null);
    if(color != null) yourView.setBackgroundColor(color);

答案 3 :(得分:0)

保存活动临时状态的最简单方法是覆盖onSaveInstanceState()。此方法接收Bundle参数。您可以向此Bundle添加数据,稍后会在活动重新开始时将其传递给您的onCreate()方法。

请注意,此过程仅在销毁并重新启动活动时发生。如果用户通过单击后退按钮返回活动,则活动可能尚未销毁。但是,这不是一个问题,因为所有活动的设置都已保留在内存中,并将在再次显示活动时使用。

答案 4 :(得分:0)

我知道这是一个老问题,但这个问题也发生在我身上。 我想突出显示所选项目并记住该选项,即使该应用已关闭并重新开始。

这是我的解决方案。 *以下所有代码都是在onCreate

上实现的
private ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, users){
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = super.getView(position, convertView, parent); //store the view on a variable, so you can use it later
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); //initialize sharedPreferences
            int id = sharedPreferences.getInt("CURRENT_ID", -1); 
            if (position == id) {
                int colorId = sharedPreferences.getInt("CURRENT_COLOR", -1);
                view.setBackgroundColor(colorId);//Here we can get access to the specific item, and set the color
            } else {view.setBackgroundColor(1);}
            return view;
        }
    };
    listView.setAdapter(adapter);

总之,从onCreate方法您无权访问特定的适配器项,您需要实现getView并使用:

view.setBackgroundColor(colorId);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener()不同 您可以通过parent.getChildAt(someIdPosition)

轻松访问

我希望这对未来的某些人有所帮助。