0

I have an app with a ListView with custom items in a ListFragment. Purpose of the app is a checklist with tasks that change background color and add a checkmark when they're selected.

Now when the task is selected it passes this info to the connected SQLite-db and reloads the adapter. With an if-statement it checks if the task is finished and if so it adds a checkmark and changes the background. This all worked fine, but when I have more tasks than the screen can hold it jumps to the top when reloading the adapter, standard behavior.

I've been experimenting with the info found on this page: Android: Is it possible to refresh just one item in a listview? And this info has been pretty helpful:

View v = listViewItems.getChildAt(position - 
        listViewItems.getFirstVisiblePosition());
v.setBackgroundColor(Color.GREEN);

My own implementation is this:

private void updateView(int index){
    View v = getListView().getChildAt(index -
            getListView().getFirstVisiblePosition());

    if(v == null)
        return;

    v.setBackgroundColor(getResources().getColor(R.color.finished_bg));

    IconTextView itv = (IconTextView) v.findViewById(R.id.task_item_icontextview);
    itv.setVisibility(View.VISIBLE);
}

Here is where it gets tricky.

1: It changes background, really nice, but when I scroll up or down and the item leaves the screen and then re-enters the background is back to it's standard color. How do I make sure that the color stays this way?

2: The checkmark doesn't show at all. Am I making wrong references? Is it even possible to change properties this way?

Community
  • 1
  • 1
Dennie
  • 743
  • 2
  • 13
  • 30

2 Answers2

0

Nevermind, I solved it!

I stopped using the updateView method and implemented the following code on the right places:

adapter.clear();
adapter.addAll(tasksList);
adapter.notifyDataSetChanged();

Adapter being the name of my customized adapter and taskList being the ArrayList with the info that should be placed in my ListView-items.

I tried doing this earlier, but made a little error.

Dennie
  • 743
  • 2
  • 13
  • 30
0
  1. use selector for the item backgrounds on pressed, selected, focused etc
    follow this simple tutorial here
  2. the checkmark is a checkbox? then add a Boolean value on the class, false as default, and set it true on list item click, or set is !boolean so it can dynamically change on each click (reverts true/false on each click). change the adapter, not the listview.
    on the adapter, check if that boolean is true, set that checbox to checked
Ferry Tan
  • 218
  • 2
  • 10
  • Thanks for the answer, but I couldn't find an option with selector to select an item and keep it selected, while also finishing other tasks. Could have experimented with accepting multiple selections, but I didn't think that would be the best way to do this. The checkmark is an IconTextView (Iconify), not a checkbox. – Dennie May 21 '14 at 10:39