简体   繁体   中英

Cannot get listitems to reload from database

I have written a small app that has a ListView with a custom adapter. Each row contains some Button s, 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 Button s back to their original colorless state. How can I get the Button s 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 (ie from database)?

You don't have to reload the whole data for every Button click.

In your Button click you're just updating the data base and not your adapter dataset values , this is why you always get the old background color.

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(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); no need for this line, getView() method will take care of the background
          //update your adapter dataset, eg: values.get(i).setB("newColor");
          notifyDataSetChanged(); // to refresh your adapter

        }
    });

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

PS: It's better if you save your "database ID" in your Model object not as a View tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM