问题描述:

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.

网友答案:

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);
相关阅读:
Top