可能重复,但不适用于我的示例: here
我有两个成员变量mSelectedOrderItem和mSelectedOrderItemId,它们都是未初始化的,因此为null。 (如果我在onCreate中将它们赋值为null,那么仍然无法工作)。还有一个ArrayList mOrderList。
在我的列表视图中,如果您选择一个项目,则会为这些变量分配与该项目对应的值。
然后我想在另一个方法onClick上使用这些赋值,但onClick总是将mSelectedOrderItem视为null,无论它是否在onItemClick中初始化,我都无法弄清楚原因。
import java.util.ArrayList;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class MenuFragment extends Fragment {
public static String TAG = "MenuFragment";
public static final String TITLE= "Menu";
private FragmentTabHost mTabHost;
private ListView mOrderListView;
private ArrayList<OrderItem> mOrderList;
private ArrayAdapter<OrderItem> mOrderListAdapter;
private OrderItem mSelectedOrderItem;
private Integer mSelectedOrderItemId;
/**
* Returns a new instance of MenuFragment for managing fragments
*
* @return a new instance of MenuFragment
*/
static MenuFragment newInstance() {
return new MenuFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.menu_fragment, container, false);
// Set up the inner food/drink tabs
mTabHost = (FragmentTabHost) layout.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.menu_items_fragment_container);
mTabHost.addTab(mTabHost.newTabSpec("food").setIndicator(FoodItemsFragment.TITLE), FoodItemsFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("drinks").setIndicator(DrinkItemsFragment.TITLE), DrinkItemsFragment.class, null);
// Set up the list view
mOrderListView = (ListView) layout.findViewById(R.id.ordered_items);
mOrderList = new ArrayList<OrderItem>();
mOrderList.add(new OrderItem("First item"));
mOrderList.add(new OrderItem("Second item"));
mOrderList.add(new OrderItem("Third item"));
mOrderList.add(new OrderItem("Fourth item"));
// Set the array adapter to the list view to display the contents of the standing order list
mOrderListAdapter = new ArrayAdapter<OrderItem>(getActivity(), android.R.layout.simple_list_item_1, mOrderList);
mOrderListView.setAdapter(mOrderListAdapter);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Set up the List View so that, when clicked, its details are shown in the details section
// Set what happens when an item in the view is clicked
mOrderListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Identify the element of the order list
// Since the list positions and the array positions are the same
// i.e. first position in the list corresponds to mOrderList.get(0)
// use that knowledge
mSelectedOrderItem = mOrderList.get(position);
mSelectedOrderItemId = position;
// Set the text of the details section
// Would it be worth storing these as member variables since they're likely to be accessed
// a lot?
((TextView) getActivity().findViewById(R.id.order_name)).setText(mSelectedOrderItem.getItemName());
((EditText) getActivity().findViewById(R.id.order_note)).setText(mSelectedOrderItem.getItemNote());
}
});
// Set what happens when the delete button is pressed
((Button) getActivity().findViewById(R.id.delete_button)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// If there's no item currently selected, do nothing
if (mSelectedOrderItem == null) {
Log.d(TAG, "mSelectedOrderItem == null");
return;
}
mOrderList.remove(mSelectedOrderItem);
mOrderListAdapter.notifyDataSetChanged();
}
});
}
}
有什么想法吗?
干杯。
编辑添加了整个班级代码。
答案 0 :(得分:0)
只需添加Log.i(“Fragment”,“mSelectedOrderItemId =”+ mSelectedOrderItemId);在你的
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
如果它将从这个方法调用正确值,在其他地方找到解决方案。
答案 1 :(得分:0)
我找到了问题的解决方案,但不一定是问题的答案。
我将整个代码体移动到我的onCreateView方法而不是我在onActivityCreated方法中的部分,因为我发现它仍在处理创建接口,所以这就是逻辑上所属的地方,瞧,它突然起作用了。
有没有人明白为什么这有效?