3 Stimmen

ListActivity: Daten aus SQLite ändern, bevor sie in die Zeile eingefügt werden

Dies ist meine ListActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_list);

    mDbHelper = new MyDbAdapter(this);
    mDbHelper.open();
    fillData();

    registerForContextMenu(getListView());
}

private void fillData() {
    // Get all of the rows from the database and create the item list
    Cursor c = mDbHelper.fetchAllTransactions();
    startManagingCursor(c);

    // Create an array to specify the fields we want to display in the list
    String[] from = new String[]{MyDbAdapter.KEY_DATE, MyDbAdapter.KEY_NAME};

    // and an array of the fields we want to bind those fields to
    int[] to = new int[]{R.id.row_date, R.id.row_name};

    // Now create a simple cursor adapter and set it to display
    setListAdapter(new SimpleCursorAdapter(this, R.layout.my_list_row, c, from, to));
}

und dies ist mein Zeilenlayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/row_date" android:layout_width="wrap_content" android:layout_height="fill_parent" />
    <TextView android:id="@+id/row_name" android:layout_width="wrap_content" android:layout_height="fill_parent" />
</LinearLayout>

Das Datum ist in der SQLLite-Datenbank als int gespeichert und wird daher als Ganzzahl angezeigt.

Kennt jemand eine Möglichkeit, ein Datum aus dem int zu erstellen, damit es nicht als 1216544611 (oder was auch immer), sondern als "12/11/2009 11:32" ?

2voto

Erich Douglass Punkte 51004

Ich vermute, dass Sie Ihr eigenes Buch schreiben müssen. CursorAdapter die den Wert int in eine Datumszeichenfolge umwandelt. Der Trick ist die Implementierung von bindView() um das Listenelement zu formatieren, bevor es angezeigt wird:

private final class MyCursorAdapter extends CursorAdapter {
    MyCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override public void bindView(View view, Context context, Cursor cursor) {             

        // Format the date string
        TextView dateView = (TextView) view.findViewById(R.id.row_date);
        long millis = cursor.getLong(cursor.getColumnIndex(MyDbAdapter.KEY_DATE));
        dateView.setText(DateFormat.getDateInstance().format(new Date(milis)));

        // Do the rest of your view formatting
        ...
    }

    @Override View public newView(Context context, Cursor cursor, ViewGroup parent) {        
        View view = getLayoutInflater().inflate(R.layout.my_list_row, null);
        bindView(view, context, cursor);
        return view;        
    }
}

und verbinden Sie es mit Ihrem Activity en onCreate() :

setListAdapter(new MyCursorAdapter(this, c));

1voto

tbruyelle Punkte 12665

Für Menschen wie mich, die sich fragen, was unter getLayoutInflater() die nicht von CursorAdapter implementiert oder geerbt wird, versuchen Sie dies:

(LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X