2

在我从当前类的 listView 中删除一个项目后,我试图从另一个片段类更新listView 。例如,我删除了一个有费用的消费者,那么该消费者的费用也将被删除。

但是消费者和费用是使用标签控件的不同页面,它是片段,当我删除消费者时,费用列表不会更新,直到我转到上一个活动,然后再次进入该页面。当消费者列表中的消费者被删除时,我该怎么做才能更新费用列表?谢谢。

这是消费者/参与者列表的代码:

public class Participant extends ListFragment implements OnClickListener{

  Intent intent;
  TextView friendId;
  Button addparticipant;
  String eventId;
  ListView lv;

public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    EventController controller = new EventController(getActivity());

    HashMap<String, String> queryValues = new HashMap<String, String>();
    Intent objIntent = getActivity().getIntent();
    eventId = objIntent.getStringExtra("eventId");
    queryValues.put("eventId", eventId);
    ArrayList<HashMap<String, String>> friendList = controller
            .getAllFriends(queryValues);
    if (friendList.size() != 0) {
        lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                friendId = (TextView) view.findViewById(R.id.friendId);
                String valFriendId = friendId.getText().toString();
                Intent objIndent = new Intent(getActivity(),
                        EventPage.class);
                objIndent.putExtra("friendId", valFriendId);
                startActivity(objIndent);
            }
        });
        lv.setOnItemLongClickListener(new OnItemLongClickListener(){

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0,
                    View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                friendId = (TextView) arg1.findViewById(R.id.friendId);
                registerForContextMenu(getListView());
                return false;
            }

        });
        SimpleAdapter adapter = new SimpleAdapter(getActivity(),
                friendList, R.layout.view_friend_entry, new String[] {
                        "friendId", "friendName", "friendSpending" },
                new int[] { R.id.friendId, R.id.friendName,
                        R.id.friendSpending });
        adapter.notifyDataSetChanged();
        setListAdapter(adapter);
    }
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.participant, container, false);
    addparticipant = (Button) rootView.findViewById(R.id.addpart);
    addparticipant.setOnClickListener(this);
    return rootView;

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent objIntent = new Intent(getActivity(),
            AddParticipant.class);
    objIntent.putExtra("eventId", eventId);
    startActivity(objIntent);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    String[] menuItems = getResources().getStringArray(R.array.menu);
    for (int i = 0; i < menuItems.length; i++) {
        menu.add(Menu.NONE, i, i, menuItems[i]);
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    int menuItemIndex = item.getItemId();
    EventController controller = new EventController(getActivity());
    switch (menuItemIndex) {
    case 0:
        String valFriendId = friendId.getText().toString();
        Intent objIndent = new Intent(getActivity(),
                EditParticipant.class);
        objIndent.putExtra("friendId", valFriendId);
        startActivity(objIndent);
        break;
    case 1:
        String valFriendId2 = friendId.getText().toString();
        controller.deleteFriend(valFriendId2);
        onResume();
    }

    return true;
}

@Override
public void onResume() {
    super.onResume();

    if (getListView() != null) {
        updateData();
    }
}

private void updateData() {
    EventController controller = new EventController(getActivity());
    HashMap<String, String> queryValues = new HashMap<String, String>();
    Intent objIntent = getActivity().getIntent();
    eventId = objIntent.getStringExtra("eventId");
    queryValues.put("eventId", eventId);
    SimpleAdapter adapter = new SimpleAdapter(getActivity(),
            controller.getAllFriends(queryValues),
            R.layout.view_friend_entry, new String[] { "friendId",
                    "friendName", "friendSpending" }, new int[] {
                    R.id.friendId, R.id.friendName, R.id.friendSpending });
    setListAdapter(adapter);
}

}
4

1 回答 1

2

使用广播:

在您的列表片段中创建此类:

public class UpdateReceiver extends BroadcastReceiver {
    public static final String NEW_UPDATE_BROADCAST =
            "your.package.NEW_UPDATE_BROADCAST";
    @Override
    public void onReceive(Context context, Intent intent) {
        //Update the list
    }
}

在片段中声明:

private UpdateReceiver updateReceiver = null;

使用它来注册接收器(当您创建片段时):

IntentFilter filter = new IntentFilter(UpdateReceiver.NEW_UPDATE_BROADCAST);
filter.addCategory(Intent.CATEGORY_DEFAULT);
updateReceiver = new UpdateReceiver();
registerReceiver(updateReceiver, filter);

使用它来取消注册(当你销毁片段时):

if (updateReceiver != null){
    unregisterReceiver(updateReceiver);
    updateReceiver = null;
} 

然后,当您想请求更新时,您可以像这样发送广播:

Intent broadcastIntent = new Intent();
broadcastIntent.setAction(UpdateReceiver.NEW_UPDATE_BROADCAST);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(broadcastIntent);

如果需要,您甚至可以在广播意图中发送数据并在 onReceive 方法中接收它......

于 2013-11-13T11:21:25.467 回答