1

I have a listview which populates its content from SQLite Database. Here's my code:

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}));

I want to put an extra String to some TextViews above when its displayed on the list. For example, I want to add a String with the word "from" on R.id.txt_org, before the populated String from the database which is Database.Project.C_ORGANIZATIONTITLE

Let's say the populated String is: New Organisation, with an extra String "from" what will be displayed is: from New Organisation

Can anybody help me with that? Thank you very much.

EDITED:

FYI, this is my SimpleCursorAdapter method:

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);
        }
    }
hectichavana
  • 1,436
  • 13
  • 41
  • 71

1 Answers1

2

Since you're already using a custom adapter, override the adapter's bindView() and newView() methods, rather than getView(). That way you will not have to manually deal with recycling the row's view.

Within these method you can get the data from the resulting Cursor and manipulate it before binding it to your row's view.

//Edit: some more code below. Note that this is just a rough outline and by no means complete or tested.

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);      
    }
}
Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116
  • thanks can show me how to do it with the code I have? I'm quite a newbie in this one – hectichavana Nov 11 '11 at 11:32
  • I added some extra code to help you get started. You would've been able to come up with exactly the same by following the two links. :) – MH. Nov 11 '11 at 11:51
  • quick question, I have a TextView which I amde limited to 100 chars,but then I want to add '...' as a string right after the 100 chars are displayed. descTextView.setText(Html.fromHtml(cursor.getString(mDescIndex)+"...")); this method doesn't work due to the limited chars I set on the TextView. Do you have any idea how I overcome this? Thanks much! :) – hectichavana Nov 11 '11 at 13:06
  • 1
    'ellipsize' is what you're looking for. You can set it either in [xml](http://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize) or [programmatically](http://developer.android.com/reference/android/widget/TextView.html#setEllipsize%28android.text.TextUtils.TruncateAt%29). You will probably want to restrict the TextView to a single line as well. – MH. Nov 11 '11 at 13:10