2015-05-06 56 views
0

在我的應用程序中,我從數據庫中讀取數據,將它放入遊標並使用適配器將它傳遞給ListView。這些數據是從1到12的數字,但我需要它們在ListView中以月份名稱的形式呈現。如何以及在哪些步驟閱讀和顯示這些數據,我可以攔截它們並從數字變爲文本?如何攔截和更改從數據庫傳遞到ListView的數據?

+0

使用SimpleCursorAdapter.setViewBinder()或使用CursorWrapper – pskink

回答

0

您可以在適配器將文本設置爲TextView之前修改數據。這將在適配器的getView方法中完成。

0

試試這個

import java.text.DateFormatSymbols; 
public String getMonth(int month) { 
    return new DateFormatSymbols().getMonths()[month-1]; 
} 
0

你可能適合一個自定義適配器!如果您的數據是遊標對象,則可以編寫自「CursorAdapter」擴展的自定義適配器類。其他方面,您可以從「BaseAdapter」擴展。 調用它由:

ShowListCursorAdapter adapter = new ShowListCursorAdapter(getActivity(), R.layout.fragment_list_detail, cursor, 
     columns, views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); 
    getListView().setAdapter(adapter); 

在定製適配器延伸的CursorAdapter: 構造:

public ShowListCursorAdapter(Context context, int layout, Cursor cursor, String[] columns, int[] views, int flag) { 
     super(context,cursor,flag); 
     mInflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

mCursor = cursor; 
     mLayout = layout; 
     mTo = views; 
     mFrom = columns; 
    } 

實施CursorAdapter的2種方法。

@Override 
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {//This will be called once 
     mView = mInflater.inflate(mLayout, viewGroup, false); 
     return mView; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) {//This is called no.of row times 
     for(int i = 0; i< mTo.length;i++) {//If you have single column no need of for loop 
      TextView content = (TextView) view.findViewById(mTo[i]); 
      content.setText(mCursor.getString(mCursor.getColumnIndex(mFrom[i])));////here you can convert number to month and display 
     } 
    } 
相關問題