2012-08-24 70 views
1

这是我当前的适配器。使用ListView删除SQLite行

这是我试图转换日期。

+0

有几种方法可以做到这一点。 (我将从最简单的开始。)你的行布局是什么样的? – Sam

+0

我使用'long'值来删除行。我将代码添加到了我的第一篇文章中。 – user1617102

+0

我在问你在ListView中使用的XML布局,这是什么布局? (如果您使用ListView的多项选择功能,这非常简单!) – Sam

回答

0

我开始改变你的适配器一个CursorAdapter但坦率地说这将是相同的,因为这SimpleCursorAdapter例子(我更新,以反映你的代码,尽我所能。):

public class Example extends Activity { 
    SimpleCursorAdapter adapter; 
    Database database; 
    ListView listView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     database = new Database(this); 
     database.open(); 

     // See my note below for a detailed explanation of this 
     Cursor cursor = database.getAllNames(); 
     adapter = new SimpleCursorAdapter(this, 
       android.R.layout.simple_list_item_checked, 
       cursor, 
       new String[] { "name" }, // "name" is the column in your database that I describe below 
       new int[] {android.R.id.text1}, 0); 

     listView = (ListView) findViewById(R.id.list); 
     listView.setAdapter(adapter); 
     listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

     Button delete = (Button) findViewById(R.id.delete_button); 
     delete.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       long[] checkedIds = listView.getCheckedItemIds(); 
       for(long id : checkedIds) 
        database.deleteName(id); 

       listView.clearChoices(); 
       adapter.changeCursor(database.getAllNames()); 
      } 
     }); 
    } 

    @Override 
    protected void onDestroy() { 
     database.close(); 
     super.onDestroy(); 
    } 
} 

好了,你的所有需求使这项工作是一种方法,它将返回一个Cursor与您的数据库类中的所有名称(我称之为getAllNames())。现在我假设您的表格模式如下所示:

CREATE TABLE Names (
    _id INTEGER PRIMARY KEY, 
    names TEXT); 

您可以相应地进行调整。你Database.getAllNames()方法应该使用这样的查询:

"SELECT _id, name FROM Names;" 

或者,如果你想要的名称的字母顺序排列:

"SELECT _id, name FROM Names ORDER BY name;" 

总之它看起来像:

public Cursor getAllNames() { 
    return NameDatabase.rawQuery("SELECT _id, name FROM Names ORDER BY name;", null); 
} 

我希望解释一些事情,老实说,这是做你想做的最简单的方法。


加上你自己的行(列表项)布局

添加自己的布局是很简单的。既然你喜欢simple_list_item_checked.xml的外观,让我们将它复制的布局和调整它为你的颜色:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:checkMark="?android:attr/textCheckMark" 
    android:gravity="center_vertical" 
    android:paddingLeft="6dip" 
    android:paddingRight="6dip" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

所有你需要补充的是:

android:background="#ffffffff" 
    android:textColor="#ff000000" 

(请注意,由于只有一个元素一个ViewGroup是没有必要的;没有LinearLayout,没有RelativeLayout等。另外"@android:id/text1"是我们在SimpleCursorAdapter android.R.id.text1中引用的id,当你改变布局时需要适当地改变它们。)

但是如果你只想反转颜色考虑使用不同的主题为你整个应用程序。打开你的清单,并添加这个主题:

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@android:style/Theme.Light" > 

默认情况下,Android使用Theme.Dark,只需将其更改为Theme.Light。现在每个项目默认都有白色背景和黑色文本!

小的调整getView()

以供将来参考,在适配器中的getView()调用getLayoutInflater()为每一个新行。您只需在构造函数中获取一次LayoutInflater并将其保存在一个变量中,可能为LayoutInflater mLayoutInflater,然后在mLayoutInflater.inflate(...)的getView()中使用它。

希望有帮助!

+0

我正在使用CustomAdapter。我将它添加到我的第一篇文章中。 – user1617102

+0

我看到你实际上扩展了一个ArrayAdapter,它不像CursorAdapter那样保留SQLite ID。 (这是你之前看到的。)让我们改变它来扩展一个CursorAdapter。 **还**你使用getView()和ViewHolder()_perfectly_。 (Upvote就是为此。) – Sam

+0

Heya,现在删除作品和ListView也起作用。感谢您向我展示SimpleCursorAdapter的魔力。 :)现在只有一个小问题。 CursorAdapter使用标准布局(android.R.layout.simple_list_item_checked),我无法用我的行布局替换它。我的意思是这不会成为问题,因为这个标准看起来也很棒。但文本的颜色似乎是(Alpha = 0),我的意思是它是隐形的,直到我点击它。然后textcolor变黑。你有任何想法如何解决这个问题? – user1617102