2012-03-28 116 views
4

我想要在ViewPager中刷新ListView,之後我從自定義對話框中刪除,添加或更新。查詢工作爲我更新,但一段時間後,它拋出一個例外..我試過notifyDataSetChanged,但沒有任何反應......我使用simplecursoradapter作爲適配器如何在ViewPager中刷新Android ListView?

任何人可以告訴我如何實現這個?

編輯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) {} 
} 
} 
+0

分享你的代碼和崩潰的棧跟蹤 – Blackbelt 2012-03-28 17:20:12

+0

添加我的代碼。有沒有崩潰因爲我刪除重新查詢... – 2012-03-28 17:47:51

回答

0

試試這一個你的適配器。

notifyDataSetChanged() 

它會在更新時刷新。

+0

我重寫'公衆詮釋getItemPosition(對象對象){ \t \t \t return POSITION_NONE; \t \t}'然後notifyDataSetChanged()被調用 – 2012-03-29 12:39:25