簡體   English   中英

在綁定列表視圖之前從數據庫中拆分字符串

[英]Split String from database before bind listview

我將一些值保存在具有以下結構的數據庫中:

Name1-Group1
Name2-Group2
...

現在,我需要加載此值以在列表視圖上顯示它們,但只需要顯示Name。 從數據庫中刪除Group值不是解決方案,因為我需要此值來進行排序。

因此,這是我從數據庫加載名稱的方式:

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

然后這就是我綁定列表視圖的方式:

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

但是在綁定它們之前,就像我之前說過的那樣,我需要獲取名稱,將它們拆分為僅具有名稱(不包含組),然后將它們綁定在listview上。

因此,我不知道該怎么做是如何從適配器獲取值以使用它們(拆分它們),然后再次在適配器中獲取它們。

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM