Android 如何拦截和更改从DB传递到ListView的数据?
在我的应用程序中,我从数据库中读取数据,将其放入游标中,并使用适配器将其传递给ListView。这些数据是从1到12的数字,但我需要它们在ListView中以月份的名称显示。如何以及在读取和显示这些数据的哪个步骤上截取它们并将其从数字更改为文本?您可以修改数据,就在它之前,适配器将文本设置为您的Android 如何拦截和更改从DB传递到ListView的数据?,android,database,listview,cursor,adapter,Android,Database,Listview,Cursor,Adapter,在我的应用程序中,我从数据库中读取数据,将其放入游标中,并使用适配器将其传递给ListView。这些数据是从1到12的数字,但我需要它们在ListView中以月份的名称显示。如何以及在读取和显示这些数据的哪个步骤上截取它们并将其从数字更改为文本?您可以修改数据,就在它之前,适配器将文本设置为您的文本视图。这将在适配器的getView方法中完成。试试这个 import java.text.DateFormatSymbols; public String getMonth(int month) {
文本视图。这将在适配器的getView
方法中完成。试试这个
import java.text.DateFormatSymbols;
public String getMonth(int month) {
return new DateFormatSymbols().getMonths()[month-1];
}
您可以为它设置一个自定义适配器!。如果您的数据是游标对象,则可以编写从“CursorAdapter”扩展而来的自定义适配器类。其他方面,您可以从“BaseAdapter”扩展。
请致电:
ShowListCursorAdapter adapter = new ShowListCursorAdapter(getActivity(), R.layout.fragment_list_detail, cursor,
columns, views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
getListView().setAdapter(adapter);
在自定义适配器扩展游标适配器中:
建造商:
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
}
}
@覆盖
public View newView(上下文上下文、光标、视图组ViewGroup){//这将被调用一次
mView=最小平坦度。充气(mLayout,viewGroup,false);
返回mView;
}
@凌驾
公共void bindView(视图视图、上下文上下文、光标){//这称为行次数
for(inti=0;i
使用SimpleCursorAdapter.setViewBinder()或使用游标包装器