0

I'm saving some values on a database which have this structure:

Name1-Group1
Name2-Group2
...

Now, I need to load this values to show them on a listview, but just I need to show the Name. Deleting the Group value from the database is not a solution, as I need this value for a sorting usability.

So, this is the way I load the names from the database:

cursor = getContentResolver().query(TravelOrderProvider.CONTENT_URI, PROJECTION, selection, arguments, order);

And then this is how I bind the listview:

ListAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor,
            new String[] {TravelOrder.NAME}, new int[] {android.R.id.text1});
listView.setAdapter(mAdapter);

But before binding them, as I said before I would need to get the names, split them to just have the name, without the group, and bind them on the listview.

So, what I don't know how to do is how to get the values from the adapter to work with them (split them) and then get them again in the adapter.

masmic
  • 3,526
  • 11
  • 52
  • 105
  • You can use setViewBinder() see http://stackoverflow.com/questions/3609126/changing-values-from-cursor-using-simplecursoradapter for more info – Shayan Pourvatan Jun 17 '14 at 08:57
  • call setViewBinder as said above or use a CursorWrapper to change one column of the Cursor – pskink Jun 17 '14 at 09:00

1 Answers1

1

try this

ListAdapter mAdapter = new SimpleCursorAdapter(this,
  android.R.layout.simple_list_item_1,
  cursor,new String[] {TravelOrder.NAME},
  new int[]{android.R.id.text1});


mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

    @Override
    public boolean setViewValue(View view, Cursor cursor,int columnIndex) {

        if(view.getId() == android.R.id.text1)
        {
          TextView tvName= (TextView) view;
          //do as you want split here 
           String[] separated = cursor.getString(columnName).split("-");
           separated[0]; // this will contain "Name"
           separated[1]; // this will contain "Group"

           tvName.setText(separated[0]);


         return true;
        }

        return false;
    }
});

listView.setAdapter(mAdapter);
MilapTank
  • 9,988
  • 7
  • 38
  • 53
  • Just one question, how do I set the String which contains the Name (separated[0]), to be the one that must be shown in the listview? – masmic Jun 17 '14 at 10:02