我已经实现了一个主要细节如下:
在左边我有一个 FragmentList,在右边我有一个与列表项目相关的表单。当我按下保存按钮时,我需要更新列表(在这种情况下,如果名称更新,列表也必须更新)。
当应用程序处于横向时,我会显示主/详细信息,如果不是,则仅显示列表,当按下名称时,我会创建一个新活动。
我的问题是按下保存按钮时我无法更新列表。notifyDataSetChanged 不起作用。
另一方面,我正在使用选项卡,因此我有一个包含 listFragment 的主 Activity,并且在 ListFragment 内部我有一个片段。
列表片段:
公共类 ListWordFragment 扩展 SherlockListFragment {
private ListWordAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_list_words, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
ListView list = getListView();
pullToRefreshAttacher = ((MainActivity) getActivity()).getAttacher();
pullToRefreshAttacher.addRefreshableView(list, this);
// Cargamos la lista de palabras
WordDAO wordsDao = new WordSqliteDAO();
Cursor mCursorWords = wordsDao.list(getSherlockActivity());
if (mCursorWords.getCount() > 0) {
String[] from = new String[] { Word.NAME, Word.TYPE, Word.TRANSLATE };
int[] to = new int[] { R.id.textView_word, R.id.textView_type,
R.id.textView_translate };
adapter = new ListWordAdapter(
getSherlockActivity(), R.layout.row_list_words,
mCursorWords, from, to, 0);
setListAdapter(adapter);
list.setItemChecked(0, true);
fillRow(list,0);
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
fillRow(l, position);
}
private void fillRow(ListView l, int position) {
Cursor cursor = (Cursor) l.getItemAtPosition(position);
String id = cursor.getString(cursor.getColumnIndex(Word.ID));
String name = cursor.getString(cursor.getColumnIndex(Word.NAME));
String type = cursor.getString(cursor.getColumnIndex(Word.TYPE));
String translate = cursor.getString(cursor.getColumnIndex(Word.TRANSLATE));
String example = cursor.getString(cursor.getColumnIndex(Word.EXAMPLE));
String note = cursor.getString(cursor.getColumnIndex(Word.NOTE));
if ((getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
&& (getResources().getString(R.string.selected_configuration).equals(Constants.CONFIGURATION_LARGE))) {
WordDetailFragment frag = (WordDetailFragment) getFragmentManager()
.findFragmentById(R.id.word_detail_fragment);
if(frag!=null){
frag.setId(id);
frag.setName(name);
frag.setType(type);
frag.setTranslate(translate);
frag.setExample(example);
frag.setNote(note);
}
} else {
Intent i = new Intent(getActivity().getApplicationContext(),
WordDetailActivity.class);
i.putExtra(Word.ID, id);
i.putExtra(Word.NAME, name);
i.putExtra(Word.TYPE, type);
i.putExtra(Word.TRANSLATE, translate);
i.putExtra(Word.EXAMPLE, example);
i.putExtra(Word.NOTE, note);
startActivity(i);
}
}
//THIS METHOD IS CALLED FROM THE MAIN ACTIVITY
public void onWordSaved() {
WordDAO wordsDao = new WordSqliteDAO();
Cursor mCursorWords = wordsDao.list(getSherlockActivity());
adapter.swapCursor(mCursorWords);
adapter.notifyDataSetChanged();
}
}
WordDetailFragment:
公共类 WordDetailFragment 扩展片段 {
//THIS IS FOR THE COMUNNICATION WITH THE MAIN ACTIVITY
OnWordSavedListener mCallback;
// Container Activity must implement this interface
public interface OnWordSavedListener {
public void onWordSaved();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnWordSavedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnWordSavedListener");
}
}
private EditText editTxt_id;
private EditText editTxt_name;
private EditText editTxt_type;
private EditText editTxt_translate;
private EditText editTxt_example;
private EditText editTxt_note;
private Button bttn_save;
public void setId(String id) {
if(id!=null){
editTxt_id.setText(id);
}
}
public void setName(String name) {
if(name!=null){
editTxt_name.setText(name);
}
}
public void setType(String type) {
if(type!=null){
editTxt_type.setText(type);
}
}
public void setExample(String example) {
if(example!=null){
editTxt_example.setText(example);
}
}
public void setTranslate(String translate) {
if(translate!=null){
editTxt_translate.setText(translate);
}
}
public void setNote(String note) {
if(note!=null){
editTxt_note.setText(note);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final Bundle arguments = getArguments();
if(arguments != null){
final String id = arguments.getString(Word.ID);
setId(id);
final String name = arguments.getString(Word.NAME);
setName(name);
final String type = arguments.getString(Word.TYPE);
setType(type);
final String translate = arguments.getString(Word.TRANSLATE);
setTranslate(translate);
final String example = arguments.getString(Word.EXAMPLE);
setExample(example);
final String note = arguments.getString(Word.NOTE);
setNote(note);
}
bttn_save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean error = false;
Toast toast;
String id = editTxt_id.getText().toString();
String name = editTxt_name.getText().toString();
String type = editTxt_type.getText().toString();
String translate = editTxt_translate.getText().toString();
String example = editTxt_example.getText().toString();
String note = editTxt_note.getText().toString();
// Verificacion de errores
if(name.equals("") || name==null){
editTxt_name.setError(getResources().getString(R.string.error_blank_field));
error = true;
}
if(type.equals("") || type==null){
editTxt_type.setError(getResources().getString(R.string.error_blank_field));
error = true;
}
if(translate.equals("") || translate==null){
editTxt_translate.setError(getResources().getString(R.string.error_blank_field));
error = true;
}
// FIN Verificacion de errores
if(!error){
WordDAO wordDao = new WordSqliteDAO();
if(id!=null && !id.equals("")){
Date updated_at = new Date();
Word word = new Word(id, name, type, translate, example, note, updated_at);
Boolean modificado = wordDao.update(getActivity(), word);
if(modificado){
toast = Toast.makeText(getActivity(), getResources().getString(R.string.ok_word_updated), Toast.LENGTH_SHORT);
toast.show();
mCallback.onWordSaved();
}else{
toast = Toast.makeText(getActivity(), getResources().getString(R.string.error_word_updated), Toast.LENGTH_SHORT);
toast.show();
}
}else{
Word word = new Word(name, type, translate, example, note);
String idRowInserted = wordDao.insert(getActivity(), word);
if(idRowInserted.equals("-1")==false){
toast = Toast.makeText(getActivity(), getResources().getString(R.string.ok_word_inserted), Toast.LENGTH_SHORT);
toast.show();
mCallback.onWordSaved();
}else{
toast = Toast.makeText(getActivity(), getResources().getString(R.string.error_word_inserted), Toast.LENGTH_SHORT);
toast.show();
}
id = idRowInserted;
}
}else{
toast = Toast.makeText(getActivity(), getResources().getString(R.string.error_form), Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
}
主要活动:
public class MainActivity extends SherlockFragmentActivity implements
ViewPager.OnPageChangeListener, TabListener, OnWordSavedListener {
private ActionBar action_bar;
private ViewPager view_pager;
private CustomPagerAdapter adapter;
private PullToRefreshAttacher pullToRefreshAttacher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new CustomPagerAdapter(getSupportFragmentManager());
view_pager = (ViewPager) findViewById(R.id.pager);
view_pager.setAdapter(adapter);
view_pager.setOnPageChangeListener(this);
action_bar = getSupportActionBar();
action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
action_bar.removeAllTabs();
action_bar.addTab(action_bar.newTab().setText(R.string.list_words)
.setTabListener(this));
action_bar.addTab(action_bar.newTab().setText(R.string.list_idioms)
.setTabListener(this));
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
view_pager.setCurrentItem(tab.getPosition());
}
@Override
public void onPageSelected(int position) {
action_bar.setSelectedNavigationItem(position);
}
public PullToRefreshAttacher getAttacher() {
return pullToRefreshAttacher;
}
//HERE WHE NOTIFY THE LISTFRAGMENT THAT THE SAVE BUTTON WAS PUSHED
@Override
public void onWordSaved() {
int index = view_pager.getCurrentItem();
Fragment currentFragment = adapter.getItem(index);
if (currentFragment instanceof ListWordFragment) {
ListWordFragment frag = (ListWordFragment) currentFragment;
frag.onWordSaved();
}
}
}
编辑:
如果我将自定义光标适配器更改为 SimpleCursorAdapter,它可以正常工作。为什么?!?
这是我的自定义适配器:
public class ListWordAdapter extends SimpleCursorAdapter {
private LayoutInflater inflater;
private Cursor mCursor;
private int mLayout;
private String[] from;
private int[] to;
public ListWordAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.mCursor = c;
this.inflater = LayoutInflater.from(context);
this.mLayout = layout;
this.from = from;
this.to = to;
}
private static class ViewHolder {
//public ImageView img;
public TextView name;
public TextView type;
public TextView translate;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(position)) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(mLayout, null);
holder = new ViewHolder();
// holder.img = (ImageView) convertView.findViewById(R.id.img_row);
holder.name = (TextView) convertView.findViewById(to[0]);
holder.type = (TextView) convertView.findViewById(to[1]);
holder.translate = (TextView) convertView.findViewById(to[2]);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(mCursor.getString(mCursor.getColumnIndex(from[0])));
holder.type.setText(mCursor.getString(mCursor.getColumnIndex(from[1])));
holder.translate.setText(mCursor.getString(mCursor.getColumnIndex(from[2])));
// holder.img.setImageResource(img_resource);
}
return convertView;
}
}