简体   繁体   中英

Android ListView strikethrough text

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

     }      
 }

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.

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