简体   繁体   English

无法从数据库重新加载listitems

[英]Cannot get listitems to reload from database

I have written a small app that has a ListView with a custom adapter. 我写了一个小应用程序,它有一个带有自定义适配器的ListView 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 每一行都包含一些Button ,它们会在点击时改变背景颜色,我也可以点击列表项目

android:descendantFocusability="blocksDescendants"

in the xml of the list items. 在列表项的xml中。 But now I have this weird bug where clicking on the list item reverts all clicked Button s back to their original colorless state. 但是现在我有这个奇怪的错误,点击列表项会将所有单击的Button恢复到原来的无色状态。 How can I get the Button s to keep their color? 我怎样才能让Button的颜色保持不变?

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): 如您所见,我更改了背景颜色并更改了数据库条目,因此当重新加载整个列表时, Button保持其颜色(我的自定义适配器的另一部分):

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): 所以我的猜测是,当单击一个项目时,列表是从原始listItem数组重新创建的,这就是为什么我试图通过重新加载我的数据库来修复它,就像这样(来自我的活动):

@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)? 如何使ListView无法在ItemClick重新加载或正确重新加载(即从数据库中)?

You don't have to reload the whole data for every Button click. 您不必为每次Button点击重新加载整个数据。

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. Button单击中,您只是更新数据库而不是适配器数据集values ,这就是您始终获得旧背景颜色的原因。

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. PS:最好将Model对象中的“数据库ID”保存为View标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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