email-style listview

Last Updated:

  1. airswit

    airswit Member This Topic's Starter

    Dec 19, 2009
    Likes Received:
    Hi all,

    I'm fairly new to android development, so sorry if this is a dumb question.
    I would like to nearly replicate the e-mail browsing list (one checkbox, as well as one or two textviews). the user can either click on the checkbox to 'select' one or more rows, or they can click on the other portion of the row to switch to a more detailed view of that row's data.
    Visually, a ListView with the android.R.layout.simple_list_item_multiple_choice layout is about right, however, i have overridden the onListItemClick method of the ListActivity to bring up the detailed information screen. when I did that, i could no longer select multiple rows.

    Can anyone help me either tweak the existing implementation, or help me write new code to achieve this effect?

    code is here for reference:
    Code (Text):
    2. public class EditRecordsMenu extends ListActivity {
    3.     @Override
    4.     public void onCreate(Bundle savedInstanceState) {
    5.         super.onCreate(savedInstanceState);
    7.         Intent i = getIntent();
    8.         if(i.getData() == null) {
    9.             i.setData(CONTENT_URI);
    10.         }
    11.         Cursor c = managedQuery(getIntent().getData(), null, null, null, null);
    12.         setListAdapter(new MyCursorAdapter(this,
    13.                 android.R.layout.simple_list_item_multiple_choice,c,
    14.                 new String[] {DATE},new int[] {}));
    15.     }
    17.     private class MyCursorAdapter extends SimpleCursorAdapter {
    19.         public MyCursorAdapter(Context context, int layout, Cursor c,
    20.                 String[] from, int[] to) {
    21.             super(context, layout, c, from, to);
    22.             setViewBinder(new MyViewBinder());
    23.         }
    25.     }
    27.     private class MyViewBinder implements
    28.     SimpleCursorAdapter.ViewBinder
    29.     {
    30.         public boolean setViewValue(View view, Cursor cursor, int columnIndex)
    31.         {
    32.             if(columnIndex==cursor.getColumnIndex(DATE)) {
    33.                 String date = MileageData.getDateFormatter().format(cursor.getLong(columnIndex));
    34.                 ((TextView)view).setText(date);
    35.                 return true;
    36.             }
    37.             return false;
    38.         }
    40.     }
    41.     protected void onListItemClick(ListView l, View v, int position, long id) {
    42.         Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);
    44.         String action = getIntent().getAction();
    45.         if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {
    46.             // The caller is waiting for us to return a note selected by
    47.             // the user.  The have clicked on one, so return it now.
    48.             setResult(RESULT_OK, new Intent().setData(uri));
    49.         } else {
    50.             // Launch activity to view/edit the currently selected item
    51.             startActivity(new Intent(Intent.ACTION_EDIT, uri));
    52.         }
    53.     }
    55. }
    Oh, I forgot to mention: I tried to change to use a custom layout, but then the onListItemClick was not being executed. was there something else i needed to add there (other than the layout (I created the layout in xml) and changing the MyCursorAdapter instantiation)?


  2. airswit

    airswit Member This Topic's Starter

    Dec 19, 2009
    Likes Received:
    so is there no way to do this? i would think one of the two solutions should be fairly trivial for most of you.

  3. achildress

    achildress New Member

    Jul 6, 2011
    Likes Received:
    I am attempting to do the same. Did you ever get any help with this? If so, can you pass on any examples?
  4. alostpacket

    alostpacket Over Macho Grande? VIP Member

    Nov 29, 2009
    Likes Received:
    I've never liked CursorAdapters to be honest.... I find it to be a bad user paradigm.... but anyways...

    What is it you are looking to achieve? I'm not sure they even use a Cursor adapter. I'd bet the use a BaseAdapter and ListView as separate classes.

    When you use a BaseAdapter, defining the view for your list item "row" becomes much easier. The separation of tasks becomes more clear, and you can see it re-using the inflated rows and understand more deeply what the API is doing under the hood.

    Try this tutorial: • View topic - Checkbox Text List :: Extension of Iconified Text tutorial

    It's one of the first ones I used why making my app Listables

    hope that helps

Share This Page