Android:在 CursorAdapter 中使用旋转文本

发布于 2024-12-13 21:20:08 字数 1538 浏览 1 评论 0原文

我有一个 ListView,里面充满了 SimpleCursorAdapter。应在此视图中显示的字段之一 (TBTsqlHelperBudgets.KEY_INTERVAL) 以 int 形式存储在数据库中(微调器的索引,用于输入)。但我想在 ListView 中显示文本而不是索引。有什么建议吗?

public class TBTTManageBudgetsActivity extends Activity {
    // ...
    // some stuff 
    // ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // ...
        // some stuff 
        // ...
                cursor = getBudgets();
                String[] uiBindFrom = { TBTsqlHelperBudgets.KEY_NAME, TBTsqlHelperBudgets.KEY_BUDGET, TBTsqlHelperBudgets.KEY_INTERVAL };               
                int[] uiBindTo = { R.id.name, R.id.budget, R.id.interval};
                cursor.moveToFirst();
                CursorAdapter adapter = new SimpleCursorAdapter(this.getApplicationContext(), R.layout.list_budgets_item, cursor, uiBindFrom, uiBindTo);                
                listView.setAdapter(adapter);
        // ...
        // some stuff 
        // ...
    }
    // ...
    // some stuff 
    // ...
}

顺便说一句:微调器是使用 xml 数组设置的:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.intervals, android.R.layout.simple_spinner_item);

使用 xml:

<resources>
    <string-array name="intervals">
        <item>per day</item>
        <item>per week</item>
        <item>per month</item>
        <item>per year</item>
    </string-array>
</resources>

I've got an ListView that is filled with an SimpleCursorAdapter. One of the Fields that should be displayed in this view (TBTsqlHelperBudgets.KEY_INTERVAL) is stored in the DB as int (index of the spinner, that is used for input). But I would like to show the text rather than the index in the ListView. Any Suggestions?

public class TBTTManageBudgetsActivity extends Activity {
    // ...
    // some stuff 
    // ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // ...
        // some stuff 
        // ...
                cursor = getBudgets();
                String[] uiBindFrom = { TBTsqlHelperBudgets.KEY_NAME, TBTsqlHelperBudgets.KEY_BUDGET, TBTsqlHelperBudgets.KEY_INTERVAL };               
                int[] uiBindTo = { R.id.name, R.id.budget, R.id.interval};
                cursor.moveToFirst();
                CursorAdapter adapter = new SimpleCursorAdapter(this.getApplicationContext(), R.layout.list_budgets_item, cursor, uiBindFrom, uiBindTo);                
                listView.setAdapter(adapter);
        // ...
        // some stuff 
        // ...
    }
    // ...
    // some stuff 
    // ...
}

By the way: The spinner is set up using an xml array:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.intervals, android.R.layout.simple_spinner_item);

with the xml:

<resources>
    <string-array name="intervals">
        <item>per day</item>
        <item>per week</item>
        <item>per month</item>
        <item>per year</item>
    </string-array>
</resources>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄟ。诗瑗 2024-12-20 21:20:08

如果您只想显示文本(而不是能够编辑它),您可以使用类似的方法来即时进行转换或查找。它只是简单地包装您的光标,然后您设置哪些列需要特殊处理。所有其他的都由底层 Cursor 对象处理:

package com.awc.util;

import java.text.DateFormat;
import java.util.Date;

import android.content.ContentResolver;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class ConversionAndLookupCursor implements Cursor {
private static final String TAG = ConversionAndLookupCursor.class.getSimpleName();


private Cursor wrapped;
private Converter converters[];


public ConversionAndLookupCursor(Cursor wrapped) {
    if(wrapped == null) {
        throw new NullPointerException("wrapped");
    }

    this.wrapped = wrapped;
    this.converters = new Converter[wrapped.getColumnCount()];
}


@Override
public void close() {
    wrapped.close();
}

@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
    Converter c;
    String s;


    Log.d(TAG, "Converting " + columnIndex);
    c = converters[columnIndex];
    if(c != null) {
        s = c.convertFrom(wrapped.getLong(columnIndex));
        buffer.data = s.toCharArray();
    } else {
        wrapped.copyStringToBuffer(columnIndex, buffer);
    }
}

@Override
public void deactivate() {
    wrapped.deactivate();
}

@Override
public byte[] getBlob(int columnIndex) {
    return wrapped.getBlob(columnIndex);
}

@Override
public int getColumnCount() {
    return wrapped.getColumnCount();
}

@Override
public int getColumnIndex(String columnName) {
    return wrapped.getColumnIndex(columnName);
}

@Override
public int getColumnIndexOrThrow(String columnName)
        throws IllegalArgumentException {
    return wrapped.getColumnIndexOrThrow(columnName);
}

@Override
public String getColumnName(int columnIndex) {
    return wrapped.getColumnName(columnIndex);
}

@Override
public String[] getColumnNames() {
    return wrapped.getColumnNames();
}

@Override
public int getCount() {
    return wrapped.getCount();
}

@Override
public double getDouble(int columnIndex) {
    return wrapped.getDouble(columnIndex);
}

@Override
public Bundle getExtras() {
    return wrapped.getExtras();
}

@Override
public float getFloat(int columnIndex) {
    return wrapped.getFloat(columnIndex);
}

@Override
public int getInt(int columnIndex) {
    return wrapped.getInt(columnIndex);
}

@Override
public long getLong(int columnIndex) {
    return wrapped.getLong(columnIndex);
}

@Override
public int getPosition() {
    return wrapped.getPosition();
}

@Override
public short getShort(int columnIndex) {
    return wrapped.getShort(columnIndex);
}

@Override
public String getString(int columnIndex) {
    Converter c;
    Long value = null;
    String s;


    c = converters[columnIndex];
    if(c != null) {
        Log.d(TAG, "Converting in getString(" + columnIndex + ")");
        if(!wrapped.isNull(columnIndex)) {
            value = wrapped.getLong(columnIndex);
        }
        s = c.convertFrom(value);
    } else {
        s = wrapped.getString(columnIndex);
    }

    return s;
}

@Override
public boolean getWantsAllOnMoveCalls() {
    return wrapped.getWantsAllOnMoveCalls();
}

@Override
public boolean isAfterLast() {
    return wrapped.isAfterLast();
}

@Override
public boolean isBeforeFirst() {
    return wrapped.isBeforeFirst();
}

@Override
public boolean isClosed() {
    return wrapped.isClosed();
}

@Override
public boolean isFirst() {
    return wrapped.isFirst();
}

@Override
public boolean isLast() {
    return wrapped.isLast();
}

@Override
public boolean isNull(int columnIndex) {
    return wrapped.isNull(columnIndex);
}

@Override
public boolean move(int offset) {
    return wrapped.move(offset);
}

@Override
public boolean moveToFirst() {
    return wrapped.moveToFirst();
}

@Override
public boolean moveToLast() {
    return wrapped.moveToLast();
}

@Override
public boolean moveToNext() {
    return wrapped.moveToNext();
}

@Override
public boolean moveToPosition(int position) {
    return wrapped.moveToPosition(position);
}

@Override
public boolean moveToPrevious() {
    return wrapped.moveToPrevious();
}

@Override
public void registerContentObserver(ContentObserver observer) {
    wrapped.registerContentObserver(observer);
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {
    wrapped.registerDataSetObserver(observer);
}

@Override
public boolean requery() {
    return wrapped.requery();
}

@Override
public Bundle respond(Bundle extras) {
    return wrapped.respond(extras);
}

@Override
public void setNotificationUri(ContentResolver cr, Uri uri) {
    wrapped.setNotificationUri(cr, uri);
}

@Override
public void unregisterContentObserver(ContentObserver observer) {
    wrapped.unregisterContentObserver(observer);
}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
    wrapped.unregisterDataSetObserver(observer);
}


////////////////////////////////////
// DATA CONVERSION STUFF
////////////////////////////////////
public void setDateConversion(int column) {
    converters[column] = new DateConverter();
}

public void setLookupConversion(int column, Cursor lookup, int lookupColumn) {
    converters[column] = new LookupConverter(lookup, lookupColumn);
}

public void clearConversion(int column) {
    converters[column] = null;
}

private interface Converter {
    public String convertFrom(Object value);
}

private class DateConverter implements Converter {
    private DateFormat fmt;


    public DateConverter() {
        fmt = DateFormat.getDateInstance(DateFormat.SHORT);
    }

    public String convertFrom(Object value) {
        String s = null;


        if(value != null && value instanceof Long) {
            s = fmt.format(new Date((Long)value));
        }

        return s;
    }
}

private class LookupConverter implements Converter {
    private Cursor lookup;
    private int lookupColumn;

    public LookupConverter(Cursor lookup, int column) {
        if(lookup == null) throw new NullPointerException("lookup");

        this.lookup = lookup;
        this.lookupColumn = column;
    }

    public String convertFrom(Object id) {
        String s = null;


        if(id != null && id instanceof Long) {
            lookup.moveToPosition(((Long)id).intValue());
            s = lookup.getString(lookupColumn);
        }

        return s;
    }
}
}

If you just want to display the text (as opposed to being able to edit it), you can use something like this to do conversions or lookups on the fly. It simply wraps your Cursor and you set which columns need special handling. All others are handled by the underlying Cursor object:

package com.awc.util;

import java.text.DateFormat;
import java.util.Date;

import android.content.ContentResolver;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class ConversionAndLookupCursor implements Cursor {
private static final String TAG = ConversionAndLookupCursor.class.getSimpleName();


private Cursor wrapped;
private Converter converters[];


public ConversionAndLookupCursor(Cursor wrapped) {
    if(wrapped == null) {
        throw new NullPointerException("wrapped");
    }

    this.wrapped = wrapped;
    this.converters = new Converter[wrapped.getColumnCount()];
}


@Override
public void close() {
    wrapped.close();
}

@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
    Converter c;
    String s;


    Log.d(TAG, "Converting " + columnIndex);
    c = converters[columnIndex];
    if(c != null) {
        s = c.convertFrom(wrapped.getLong(columnIndex));
        buffer.data = s.toCharArray();
    } else {
        wrapped.copyStringToBuffer(columnIndex, buffer);
    }
}

@Override
public void deactivate() {
    wrapped.deactivate();
}

@Override
public byte[] getBlob(int columnIndex) {
    return wrapped.getBlob(columnIndex);
}

@Override
public int getColumnCount() {
    return wrapped.getColumnCount();
}

@Override
public int getColumnIndex(String columnName) {
    return wrapped.getColumnIndex(columnName);
}

@Override
public int getColumnIndexOrThrow(String columnName)
        throws IllegalArgumentException {
    return wrapped.getColumnIndexOrThrow(columnName);
}

@Override
public String getColumnName(int columnIndex) {
    return wrapped.getColumnName(columnIndex);
}

@Override
public String[] getColumnNames() {
    return wrapped.getColumnNames();
}

@Override
public int getCount() {
    return wrapped.getCount();
}

@Override
public double getDouble(int columnIndex) {
    return wrapped.getDouble(columnIndex);
}

@Override
public Bundle getExtras() {
    return wrapped.getExtras();
}

@Override
public float getFloat(int columnIndex) {
    return wrapped.getFloat(columnIndex);
}

@Override
public int getInt(int columnIndex) {
    return wrapped.getInt(columnIndex);
}

@Override
public long getLong(int columnIndex) {
    return wrapped.getLong(columnIndex);
}

@Override
public int getPosition() {
    return wrapped.getPosition();
}

@Override
public short getShort(int columnIndex) {
    return wrapped.getShort(columnIndex);
}

@Override
public String getString(int columnIndex) {
    Converter c;
    Long value = null;
    String s;


    c = converters[columnIndex];
    if(c != null) {
        Log.d(TAG, "Converting in getString(" + columnIndex + ")");
        if(!wrapped.isNull(columnIndex)) {
            value = wrapped.getLong(columnIndex);
        }
        s = c.convertFrom(value);
    } else {
        s = wrapped.getString(columnIndex);
    }

    return s;
}

@Override
public boolean getWantsAllOnMoveCalls() {
    return wrapped.getWantsAllOnMoveCalls();
}

@Override
public boolean isAfterLast() {
    return wrapped.isAfterLast();
}

@Override
public boolean isBeforeFirst() {
    return wrapped.isBeforeFirst();
}

@Override
public boolean isClosed() {
    return wrapped.isClosed();
}

@Override
public boolean isFirst() {
    return wrapped.isFirst();
}

@Override
public boolean isLast() {
    return wrapped.isLast();
}

@Override
public boolean isNull(int columnIndex) {
    return wrapped.isNull(columnIndex);
}

@Override
public boolean move(int offset) {
    return wrapped.move(offset);
}

@Override
public boolean moveToFirst() {
    return wrapped.moveToFirst();
}

@Override
public boolean moveToLast() {
    return wrapped.moveToLast();
}

@Override
public boolean moveToNext() {
    return wrapped.moveToNext();
}

@Override
public boolean moveToPosition(int position) {
    return wrapped.moveToPosition(position);
}

@Override
public boolean moveToPrevious() {
    return wrapped.moveToPrevious();
}

@Override
public void registerContentObserver(ContentObserver observer) {
    wrapped.registerContentObserver(observer);
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {
    wrapped.registerDataSetObserver(observer);
}

@Override
public boolean requery() {
    return wrapped.requery();
}

@Override
public Bundle respond(Bundle extras) {
    return wrapped.respond(extras);
}

@Override
public void setNotificationUri(ContentResolver cr, Uri uri) {
    wrapped.setNotificationUri(cr, uri);
}

@Override
public void unregisterContentObserver(ContentObserver observer) {
    wrapped.unregisterContentObserver(observer);
}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
    wrapped.unregisterDataSetObserver(observer);
}


////////////////////////////////////
// DATA CONVERSION STUFF
////////////////////////////////////
public void setDateConversion(int column) {
    converters[column] = new DateConverter();
}

public void setLookupConversion(int column, Cursor lookup, int lookupColumn) {
    converters[column] = new LookupConverter(lookup, lookupColumn);
}

public void clearConversion(int column) {
    converters[column] = null;
}

private interface Converter {
    public String convertFrom(Object value);
}

private class DateConverter implements Converter {
    private DateFormat fmt;


    public DateConverter() {
        fmt = DateFormat.getDateInstance(DateFormat.SHORT);
    }

    public String convertFrom(Object value) {
        String s = null;


        if(value != null && value instanceof Long) {
            s = fmt.format(new Date((Long)value));
        }

        return s;
    }
}

private class LookupConverter implements Converter {
    private Cursor lookup;
    private int lookupColumn;

    public LookupConverter(Cursor lookup, int column) {
        if(lookup == null) throw new NullPointerException("lookup");

        this.lookup = lookup;
        this.lookupColumn = column;
    }

    public String convertFrom(Object id) {
        String s = null;


        if(id != null && id instanceof Long) {
            lookup.moveToPosition(((Long)id).intValue());
            s = lookup.getString(lookupColumn);
        }

        return s;
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文