如何在ViewPager中刷新Android ListView?

时间:2012-03-28 17:16:11

标签: android listview simplecursoradapter android-viewpager

我希望在从自定义对话框中删除,添加或更新后,在ViewPager中刷新ListView。 requery为我工作更新,但经过一段时间后抛出异常..我试过notifyDataSetChanged但没有任何反应......我使用simplecursoradapter作为适配器

任何人都可以告诉我如何实现这个目标吗?

编辑1:

public class MainActivity extends Activity {

private DatabaseAdapter db;

private ViewPager myPager;
private static int NUM_MY_VIEWS = 4;
private MYView myView;

private ListView listView1, listView2, listView3, listView4;
private Cursor cursor1, cursor2, cursor3, cursor4;
private SimpleCursorAdapter adapter1, adapter2, adapter3, adapter4;

Context ctx = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d("DEBUG", "Create");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    db = new DatabaseAdapter();

    myView = new MyView();
    myPager = (ViewPager) findViewById(R.id.pager);
    myPager.setAdapter(myView);
    myPager.setCurrentItem(1);
}

@Override
protected void onDestroy() {
    Log.d("DEBUG", "Destroy");
    super.onDestroy();
    db.close();
}

@Override
protected void onPause() {
    Log.d("DEBUG", "Pause");
    super.onPause();
}

@Override
protected void onRestart() {
    Log.d("DEBUG", "Restart");
    super.onRestart();
}

@Override
protected void onResume() {
    Log.d("DEBUG", "Resume");
    super.onResume();
}

@Override
protected void onStart() {
    Log.d("DEBUG", "Start");
    super.onStart();
    db.open();
}

@Override
protected void onStop() {
    Log.d("DEBUG", "Stop");
    super.onStop();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if(resultCode == RESULT_OK) {
        fillData();
    }
}

/***********************************************************************/

private void fillData() {
    // TODO
    Log.d("DEBUG", "fillData");

    adapter1.notifyDataSetChanged
    adapter2.notifyDataSetChanged();
    adapter3.notifyDataSetChanged();
    adapter4.notifyDataSetChanged();
    notePager.getAdapter().notifyDataSetChanged();
}

/*
 * click listener
 */ 

private void onClickDeleteNote(long id) {
    final long elemID = id;

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.dialogTitleDelete))
        .setMessage(getString(R.string.dialogMessageDelete))
        .setIcon(R.drawable.ic_dialog)
        .setCancelable(true)
        .setPositiveButton(getString(R.string.dialogButtonDeletePositive), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                if(db.deleteElem(elemID)) {
                    fillData();
                } else {
                    Toast.makeText(getApplicationContext(), getString(R.string.dialogToastError), Toast.LENGTH_SHORT).show();
                }
            }
        })
        .setNegativeButton(getString(R.string.dialogButtonDeleteNegative), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
    builder.show();
}

/*
 * Pager
 */

private class MyView extends PagerAdapter { // TODO

    @Override
    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = null;
        switch (position) {
            case 0: // Tags
            {
                view = inflater.inflate(R.layout.view1, null);

                cursor1 = db.fetchData(0);
                startManagingCursor(cursor1);

                String[] from = new String[]{ DatabaseAdapter.TAG_TITLE };
                int[] to = new int[]{ android.R.id.text1 };

                adapter1 = new SimpleCursorAdapter(ctx, android.R.layout.simple_list_item_1, cursor1, from, to);

                listView1 = (ListView) view.findViewById(R.id.listView1);
                listView1.setAdapter(adapter1);
                break;
            }
            case 1: // Notes
            {
                view = inflater.inflate(R.layout.view2, null);

                cursor2 = db.fetchData();
                startManagingCursor(cursor2);

                String[] from = new String[]{ DatabaseAdapter.MY_TITLE, DatabaseAdapter.MY_CONTENT };
                int[] to = new int[]{ R.id.Title, R.id.Content };

                adapter2 = new SimpleCursorAdapter(ctx, R.layout.my_row, cursor2, from, to);
                adapter2.setViewBinder(new MyViewBinder());

                listView2 = (ListView) view.findViewById(R.id.listView2);
                listView2.setAdapter(adapter2);
                break;
            }
            case 2: // notes Started
            {
                //same as in case 1
                break;
            }
            case 3: // notes Favorite
            {
                // same as in case 1
                break;
            }
        }

        ((ViewPager) collection).addView(view, 0);

        return view;
    }

    @Override
    public int getCount() {
        return NUM_MY_VIEWS;
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((View) view);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((View)object);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {}

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View view) {}

    @Override
    public void finishUpdate(View view) {}
}
}

1 个答案:

答案 0 :(得分:0)

在你的适配器上试试这个。

notifyDataSetChanged()

它将在更新时刷新。