【问题标题】:Cannot get listitems to reload from database无法从数据库中获取要重新加载的列表项
【发布时间】:2016-05-29 21:32:42
【问题描述】:

我编写了一个小应用程序,它有一个带有自定义适配器的ListView。每行包含一些Buttons,单击时会更改背景颜色,并且我通过放置

使列表项也可以单击
android:descendantFocusability="blocksDescendants"

在列表项的 xml 中。但是现在我有一个奇怪的错误,点击列表项会将所有点击的Buttons 恢复到原来的无色状态。如何让Buttons 保持颜色?

详情:

部分自定义适配器:

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);

    }
};

如您所见,我更改了背景颜色并更改了数据库条目,因此当重新加载整个列表时,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;
}

当去另一个活动并回到这个活动时,这很好用。按钮按预期创建颜色。

所以我的猜测是,当单击一个项目时,该列表是从原始 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();
        }
    });

}

但这不起作用。

我怎样才能让ListViewItemClick 上不重新加载或正确重新加载(即从数据库)?

【问题讨论】:

    标签: java android sqlite listview


    【解决方案1】:

    您不必为每次Button 点击重新加载整个数据。

    在您的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:最好将“数据库 ID”保存在模型对象中而不是 View 标记中。

    【讨论】:

    • 这行得通!它还提高了可维护性,因为我现在将所有颜色集中在一个地方。 ……但是我不是很懂PS,我应该把id放在哪里?
    • 您的 是什么(项目类别)的列表?
    • 啊,好的,项目类当然存储DBid,但是为了能够在onClick 方法中访问它,我将它放入标签中。现在我想了想,把i 放在那里更有意义,这样我就可以使用values.get(i).getId() 来访问它。我已将标签更改为long[] 以传递DBid i,这当然毫无意义:D 感谢您为我指明正确的方向!
    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2018-09-30
    • 2011-10-18
    • 2014-07-14
    • 1970-01-01
    相关资源
    最近更新 更多