Android ListView 删除线文本
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.