Android ListView 删除线文本

发布于 2024-12-11 01:27:35 字数 1300 浏览 1 评论 0原文

我正在 Android 中制作一个购物清单,为此我将产品存储在 SQLite 数据库中。

产品是可点击的,因此应该有删除线。更新数据库工作正常,但没有在正确的项目上添加删除线。 /** * 从数据库加载列表,将数据放入列表视图并删除不活动的产品 */

public void loadListe()
{
//---get all titles---
  meineListe.clear();
  adapter.clear();     

  db.open();
  Cursor c = db.getAllTitles();
  ArrayList<String> strikedItems = new ArrayList<String>();
  strikedItems.clear();
  if (c.moveToFirst())
  {
      do {               
            if (c.getInt( 0 )==1) {
              meineListe.add(c.getString( 1 ));
              strikedItems.add( c.getString( 1 ) );
            }
            else meineListe.add( c.getString( 1 ));
      } while (c.moveToNext());
  }
  db.close();       
  //Strike Through on inactive Items
  list = this.getListView();
  for (int i = 0; i < list.getChildCount(); i++) {

    LinearLayout row = (LinearLayout) list.getChildAt( i );
    TextView title;
    title = (TextView) row.getChildAt( 0 );
    //System.out.println("zeile "+i+ " "+title.getText());

    if(strikedItems.contains( title.getText() ))
    {
      title.setPaintFlags( Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
      System.out.println("zeile "+i+ " Title: "+title.getText());
      title.setTextColor(0xFFFF0000); //black

     }      
 }

I'm doing a Shopping List in Android, for which I'm storing Products in SQLite Database.

The Products are Clickable so that they should get strikethrough. Updating Database works fine but the strikethrough is not made on the correct item.
/**
* Loads the List from DB, puts Data into Listview and strikes through inactive Products
*/

public void loadListe()
{
//---get all titles---
  meineListe.clear();
  adapter.clear();     

  db.open();
  Cursor c = db.getAllTitles();
  ArrayList<String> strikedItems = new ArrayList<String>();
  strikedItems.clear();
  if (c.moveToFirst())
  {
      do {               
            if (c.getInt( 0 )==1) {
              meineListe.add(c.getString( 1 ));
              strikedItems.add( c.getString( 1 ) );
            }
            else meineListe.add( c.getString( 1 ));
      } while (c.moveToNext());
  }
  db.close();       
  //Strike Through on inactive Items
  list = this.getListView();
  for (int i = 0; i < list.getChildCount(); i++) {

    LinearLayout row = (LinearLayout) list.getChildAt( i );
    TextView title;
    title = (TextView) row.getChildAt( 0 );
    //System.out.println("zeile "+i+ " "+title.getText());

    if(strikedItems.contains( title.getText() ))
    {
      title.setPaintFlags( Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
      System.out.println("zeile "+i+ " Title: "+title.getText());
      title.setTextColor(0xFFFF0000); //black

     }      
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖伴 2024-12-18 01:27:35

ListView 的子视图只是当时可见的那些项目,而不是整个项目数组。

您不需要像这样迭代它们,您需要从 Adapter.getView() 实现中设置删除线属性。

The child Views of the ListView are only those items which are visible at the time, not the whole array of items.

You don't iterate them like this, you need to set the strikeout property from your Adapter.getView() implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文