我尝试从当前类中删除listView中的项目后,从另一个片段类更新listView。例如,我删除了有费用的消费者,那么该消费者的费用也将被删除。但是消费者和费用是使用标签控件的不同页面,这是片段,当我删除消费者时,费用列表不会更新,直到我转到上一页,然后再次进入页面。当删除消费者列表中的消费者时,如何更新费用列表?我听说使用广播接收器能够做到这一点,但我怎么能在我的代码中实现它?我是android新手,请指导我。谢谢
以下是消费者/参与者列表的代码:
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);
}
}
以下是费用清单的代码:
public class Expense extends ListFragment implements OnClickListener {
Button addexp;
TextView expenseId;
ListView lv;
String eventId, friendId;
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>> expenseList = controller
.getAllExpenses(queryValues);
if (expenseList.size() != 0) {
lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
expenseId = (TextView) view.findViewById(R.id.expenseId);
String valExpenseId = expenseId.getText().toString();
Intent objIndent = new Intent(getActivity(),
EditExpense.class);
objIndent.putExtra("expenseId", valExpenseId);
startActivity(objIndent);
}
});
SimpleAdapter adapter = new SimpleAdapter(getActivity(),
expenseList, R.layout.view_expense_entry, new String[] {
"expenseId", "expenseName","expenseQuantity" }, new int[] {
R.id.expenseId, R.id.expenseName, R.id.expenseQuantity });
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.expense, container, false);
addexp = (Button) rootView.findViewById(R.id.addexp);
addexp.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent objIntent = new Intent(getActivity(), AddExpense.class);
objIntent.putExtra("eventId", eventId);
startActivity(objIntent);
}
@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.getAllExpenses(queryValues),
R.layout.view_expense_entry, new String[] { "expenseId",
"expenseName", "expenseQuantity" }, new int[] { R.id.expenseId,
R.id.expenseName, R.id.expenseQuantity });
setListAdapter(adapter);
}
}
答案 0 :(得分:2)
与我相同的情况,如果您使用FragmentPagerAdapter
,请在FirstFragment
private void updateSecondFragment(){
//Way to get TagName which generated by FragmentPagerAdapter
String tagName = "android:switcher:" + R.id.pager + ":" + 1; // Your pager name & tab no of Second Fragment
//Get SecondFragment object from FirstFragment
SecondFragment f2 = (SecondFragment)getActivity().getSupportFragmentManager().findFragmentByTag(tagName);
//Then call your wish method from SecondFragment to update appropriate list
f2.updateList();
}
我通过阅读this得到了这个想法。然后编辑一点!
答案 1 :(得分:0)
一种方法是在包含两个片段的活动中创建方法,并从活动中访问片段方法。您可以使用相同的方法更新数据库并重新加载列表。
例如,在活动中
public void updateFragment2(){ ... }
在片段1中,您应该致电:
((ActivityClass)getActivity()).updateFragment2();
在“与活动通信”部分中,您可以看到此示例的说明: http://developer.android.com/guide/components/fragments.html