2015-09-09 53 views
0

我有一個列表視圖,它顯示任務列表,在選擇任務時它顯示任務的詳細信息,當我刪除它返回到finish()時的前一個任務的特定任務時。但它不會更新列表。 我想知道如何以及在哪裏使用notifyDataSetChanged方法,並且從不使用添加適配器方法。 除了notifyDataSetChanged()解決方案也被接受:)我只想在它返回到前一個活動時更新列表。如何使用notifyDataSetChanged與finish()

回答

1

startActivityForResult()一起做。當你創建意圖打開新的活動打開它的結果。被刪除的任務是你的結果。因此,當它被標記爲已刪除且您返回到之前的活動時,結果會觸發,您可以刪除標記的項目+呼叫notify

此處瞭解詳情:http://developer.android.com/training/basics/intents/result.html

0

可以使用notifyDataSetChanged上的onResume方法,當活動顯示

0

您也可以完成你的當前活動前通知父活動這個時刻更新數據本身。您只需在主要活動中註冊Receiver,其他所有活動都可以通知該活動。你甚至可以發送數據!

由於我的英文並不好,示例代碼: MainActivity類別

public class MainActivity extends AppCompatActivity { 

// you can define your name for your receiver as a constant, 
// so you can access it from other activities if you want 
public static final String MY_SUPER_INTERNAL_NOTIFICATION = "MY_SUPER_INTERNAL_NOTIFICATION"; 
public static final String MY_OBJECT = "my_object"; 
public static final String MY_OBJECT_POSITION = "my_object_position" ; 

private MyCustomAdapter adapter; 
private ListView listView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    listView = (ListView)findViewById(R.id.listView); 
    // set up your list with your adapter and data... 
    adapter = new MyCustomAdapter(this); 
    // [...] I suppose you know how to do that, I dont write everything 
    listView.setAdapter(adapter); 

    // when you click on a row from your list, 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Intent intent = new Intent(
        MainActivity.this, 
        SecondActivity.class 
      ); 

      // you can for example put data in a Bundle and pass it to the other activity 
      // then on the onCreate you can use that data as you want 
      Bundle bundle = new Bundle(); 
      // IMPORTANT be sure that the object you are putting in the bundle is Serializable (implements Serializable) 
      bundle.putSerializable(MY_OBJECT, adapter.getItem(position)); 
      // you can also for example send the position of the row you clicked 
      bundle.putInt(MY_OBJECT_POSITION, position); 

      // put your bundle in the intent 
      intent.putExtras(bundle); 

      startActivity(intent); 
     } 
    }); 

    // register your Receiver! don't forget to do that or you will never be notified 
    LocalBroadcastManager 
      .getInstance(this) 
      .registerReceiver(
        broadcastReceiver, 
        new IntentFilter(MY_SUPER_INTERNAL_NOTIFICATION) 
      ); 
} 

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent != null) { 
      // get the position sent by the other activity 
      int position = intent 
        .getExtras() 
        .getInt(SecondActivity.MY_EXAMPLE_KEY); 

      adapter.deleteItem(position); 
     } 
    } 
}; 

// adapter class... 
private class MyCustomAdapter extends BaseAdapter { 

    ArrayList<MyObject> data; 

    public MyCustomAdapter(Context context) { 

    } 

    public void deleteItem(int position){ 
     // delete your item from the list of data 
     data.remove(position); 
     // dont forget to notify 
     notifyDataSetChanged(); 
    } 

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

    @Override 
    public MyObject getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     return null; 
    } 
    // adapter class, you can extend your favorite type of adapter 
} 
} 

的SecondActivity類:

public class SecondActivity extends AppCompatActivity { 

public static final String MY_EXAMPLE_KEY = "EXAMPLE"; 

private MyObject myObject; 
private int myObjectPosition; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second_activity); 

    myObject = (MyObject) getIntent() 
      .getExtras() 
      .getSerializable(MainActivity.MY_OBJECT); 

    myObjectPosition = getIntent() 
      .getExtras() 
      .getInt(MainActivity.MY_OBJECT_POSITION); 

    // do all your stuff with your object 
    // before calling finish do this 
    beforeFinishDoThisStuff(); 
} 

private void beforeFinishDoThisStuff() { 
    sendBroadcastToMainActivity(); 
    finish(); 
} 

private void sendBroadcastToMainActivity() { 
    // create an intent and put your Receiver name as action name 
    // like you defined in your MainActivity 
    Intent intent = new Intent(MainActivity.MY_SUPER_INTERNAL_NOTIFICATION); 

    Bundle bundle = new Bundle(); 
    // put whatever you want, here I put just the previous position of the object in the list 
    bundle.putInt(MY_EXAMPLE_KEY, myObjectPosition); 
    intent.putExtras(bundle); 

    // notify your MainActivity 
    LocalBroadcastManager 
      .getInstance(this) 
      .sendBroadcast(intent); 

    // after this finish ! 
} 
} 

希望你瞭解如何與BroadcastManager通知活動。它非常強大且易於使用。

玩得開心編碼!

相關問題