简体   繁体   中英

Split String from database before bind listview

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM