mercredi 17 février 2016

Button coloring changes back when listViewItem is clicked

I have written a small app that has a listView with a custom adapter. Each row contains some buttons, which will change background color when clicked, and I got the list items to be clickable as well by putting

android:descendantFocusability="blocksDescendants"

in the xml of the list items. But now I have this weird bug where clicking on the list item reverts all clicked buttons back to their original colorless state. How can I get the buttons to keep their color?

Details:

Part of the custom adapter:

View.OnClickListener onButtonClicked = new View.OnClickListener() {
    @Override
    public void onClick(View button) {
        View listItem = (View) button.getParent();
        final long DBid = (long) listItem.getTag();//database ID

        final Button b = (Button) button;

                    sqldataDataSource datasource = new sqldataDataSource(context);
                    datasource.open();
                    datasource.updateButton(DBid);
                    datasource.close();
                    b.setBackgroundColor(0xFF386F00);

    }
};

As you can see, I change the background color AND change the database entry, so when the whole list is reloaded, the button keeps its color (another part of my custom adapter):

public View getView(int i, View convertView, ViewGroup parent) {
    LayoutInflater inflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.hrlistitems, parent, false);

    Button b = (Button) rowView.findViewById(R.id.HRlistB);
    b.setOnClickListener(onButtonClicked);

    if(!(values.get(i).getB().equals(""))){
        b.setBackgroundColor(0xFF386F00);
    }
    return rowView;
}

This works fine when going to another activity and coming back to this one. The buttons are created colored as expected.

So my guess was that the list is recreated from the original listItem array when an item is clicked, which is why I tried to fix this by reloading my database, like so (from my activity):

@Override
protected void onStart() {
    super.onStart();

    datasource = new sqldataDataSource(this);
    datasource.open();

    listItems = datasource.getOnlyRoutes(id);//this works fine
    Collections.sort(listItems, HallenRoute.vergleichen());

    if (mListView == null) {
        mListView = (ListView) findViewById(R.id.listViewHalle);
    }
    adapter=new customAdapter(this, listItems);
    setListAdapter(adapter);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int pos, long nid) {
            listItems.get(pos).increaseCount();
            datasource.updateCountHR(listItems.get(pos));
            listItems = datasource.getOnlyRoutes(id);//fix I tried, doesn't work
            Collections.sort(listItems, HallenRoute.vergleichen());
            adapter.notifyDataSetChanged();
        }
    });

}

But this doesn't work. How can I get the listView to either not reload on ItemClick or reload properly (i.e. from database)?

Aucun commentaire:

Enregistrer un commentaire