Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I started changing your adapter to a CursorAdapter but honestly it would be the same as this SimpleCursorAdapter example (I updated it to reflect your code as best I could.):</p> <pre><code>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(); } } </code></pre> <p>Ok, all your need to make this work is a method that returns a Cursor with all of the names (I called it <code>getAllNames()</code>) in your Database class. Now I assumed your table schema looks something like this:</p> <pre><code>CREATE TABLE Names ( _id INTEGER PRIMARY KEY, names TEXT); </code></pre> <p>You can adjust accordingly. Your <code>Database.getAllNames()</code> method should use a query like this:</p> <pre><code>"SELECT _id, name FROM Names;" </code></pre> <p>Or if you want the names alphabetized:</p> <pre><code>"SELECT _id, name FROM Names ORDER BY name;" </code></pre> <p>All together it will look like:</p> <pre><code>public Cursor getAllNames() { return NameDatabase.rawQuery("SELECT _id, name FROM Names ORDER BY name;", null); } </code></pre> <p>I hope that explains a few things, honestly this is the easiest way to do what you want.</p> <hr> <p><strong>Adding your own row (list item) layout</strong></p> <p>Adding your own layout is simple enough. Since you like the look of simple_list_item_checked.xml, let's copy it's layout and tweak it for your colors:</p> <pre><code>&lt;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" /&gt; </code></pre> <p>All you need to add is:</p> <pre><code> android:background="#ffffffff" android:textColor="#ff000000" </code></pre> <p>(Notice that since there is only one element a ViewGroup is not necessary; no LinearLayout, no RelativeLayout, etc. Also <code>"@android:id/text1"</code> is the id that we referenced in the SimpleCursorAdapter `android.R.id.text1', when you change layouts you need to change these appropriately.)</p> <p><strong>However</strong> if you only want to invert the colors consider using a different Theme for you entire app. Open up your Manifest and add this theme:</p> <pre><code>&lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.Light" &gt; </code></pre> <p>By default Android uses Theme.Dark, simply change it to Theme.Light. Now every item has a white background and black text by default!</p> <p><strong>Minor Adjustment to getView()</strong></p> <p>For future reference, in your adapter's getView() you call getLayoutInflater() for every new row. You only need to get the LayoutInflater once in the constructor and save it in a variable, possibly <code>LayoutInflater mLayoutInflater</code>, and use this in getView() with <code>mLayoutInflater.inflate(...)</code>. </p> <p>Hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload