【问题标题】:Android: adding an extra String to TextView which populates its content from DatabaseAndroid:向 TextView 添加一个额外的字符串,该字符串从数据库填充其内容
【发布时间】:2011-11-11 10:58:21
【问题描述】:

我有一个从 SQLite 数据库填充其内容的列表视图。 这是我的代码:

ListView listView = (ListView) findViewById(R.id.lstText);
        listView.setOnItemClickListener(this);
        listView.setAdapter(new MySimpleCursorAdapter(this, R.layout.listitems,
                managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
                        Database.Project.NAME), new String[] { BaseColumns._ID,
                        Database.Project.C_PROJECTTITLE,
                        Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE}, null, null, null),
                new String[] { Database.Project.C_PROJECTTITLE,
                        Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE}, new int[] {
                        R.id.txt_title, R.id.image, R.id.txt_list_desc, R.id.txt_org}));

当它显示在列表中时,我想在上面的一些TextViews 上添加一个额外的字符串。例如,我想在来自数据库的填充字符串 Database.Project.C_ORGANIZATIONTITLE 之前在 R.id.txt_org 上添加一个带有单词“from”的 String

假设填充的String 是:新组织, 加上一个额外的String“来自”,将显示的是:来自新组织

有人可以帮我吗?非常感谢。

已编辑:

仅供参考,这是我的 SimpleCursorAdapter 方法:

class MySimpleCursorAdapter extends SimpleCursorAdapter {

        public MySimpleCursorAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {
            super(context, layout, c, from, to);
            loader = new ImageLoader(context);
            this.context = context;
        }

        Context context=null;
        ImageLoader loader = null;

        public void setViewImage(ImageView v, String value) {
            v.setTag(value);
            loader.DisplayImage(value, context, v);
        }
    }

【问题讨论】:

  • 刚刚更新了我的问题,你能看一下吗?我没有 getView 方法。我该怎么做?

标签: android


【解决方案1】:

由于您已经在使用自定义适配器,请覆盖适配器的 bindView()newView() 方法,而不是 getView()。这样您就不必手动处理回收行的视图。

在这些方法中,您可以从生成的光标中获取数据并在将其绑定到行视图之前对其进行操作。

//编辑:下面还有一些代码。请注意,这只是一个粗略的大纲,绝不是完整的或经过测试的。

class MySimpleCursorAdapter extends SimpleCursorAdapter {

    private ImageLoader mLoader = null;
    private LayoutInflater mInflater = null;
    private int mBusinessNameIndex = -1;
    private int mSmallImageIndex = -1;

    public MySimpleCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
        mLoader = new ImageLoader(context);
        mInflater = getLayoutInflater();    
        mBusinessNameIndex = c.getColumnIndexOrThrow(Database.Project.NAME);
        mSmallImageIndex = c.getColumnIndexOrThrow(Database.Project.C_SMALLIMAGE);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return mInflater.inflate(R.layout.row, null);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // Get your views from 'view'
        TextView someTextView = (TextView) view.findViewById(R.id.xxx);
        ImageView someImageView = (ImageView) view.findViewById(R.id.yyy);
        // Set the data
        someTextView.setText("from " + cursor.getString(mBusinessNameIndex));
        mLoader.DisplayImage(cursor.getString(mSmallImageIndex ), context, someImageView);      
    }
}

【讨论】:

  • 谢谢 可以告诉我如何使用我拥有的代码吗?我是这方面的新手
  • 我添加了一些额外的代码来帮助您入门。通过遵循这两个链接,您将能够得出完全相同的结果。 :)
  • 快速问题,我有一个 TextView,我限制为 100 个字符,但是我想在显示 100 个字符后立即添加 '...' 作为字符串。 descTextView.setText(Html.fromHtml(cursor.getString(mDescIndex)+"..."));由于我在 TextView 上设置的字符有限,此方法不起作用。你知道我是如何克服这个问题的吗?非常感谢! :)
  • 'ellipsize' 是您正在寻找的。您可以在xmlprogrammatically 中设置它。您可能还希望将 TextView 限制为一行。
猜你喜欢
  • 2011-03-08
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多