我正在尝试学习片段,我正在使用在eclipse中创建的Multiform项目模板。我在onListItemClick.
中传递数据时遇到问题我将数据加载到SQL数据库中这是一个名称和地址列表。在列表视图中,我列出了名称,并且我希望在ListView
中单击名称时列出名称和地址。可能有更好的方法来做到这一点,但我只是从这开始。
public class CustomerListFragment extends ListFragment {
private Callbacks mCallbacks = sDummyCallbacks;
public interface Callbacks {
public void onItemSelected(String id);
}
private static Callbacks sDummyCallbacks = new Callbacks() {
public void onItemSelected(String id) {
}
};
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
long myLong = values.get(position).getId();
mCallbacks.onItemSelected(String.valueOf(myLong));
}
在Detail片段中,我有以下内容。即使我从数据中传递Id,ARG_ITEM_ID也始终具有组件的id值。
public class CustomerDetailFragment extends Fragment {
public static final String ARG_ITEM_ID = "_id";
DataSource cusdatasource;
sqlCustomer selectedCustomer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
datasource = new DataSource(getActivity());
datasource.open();
if (getArguments().containsKey(ARG_ITEM_ID)) {
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
}
selectedCustomer = datasource.getCustomer("here I need the _id from the list item clicked");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_detail_customer, container, false);
if (selectedCustomer != null) {
((TextView) rootView.findViewById(R.id.editText1)).setText(selectedCustomer.getName());
((TextView) rootView.findViewById(R.id.editText2)).setText(selectedCustomer.getStreet());
((TextView) rootView.findViewById(R.id.editText3)).setText(selectedCustomer.getCitySZ());
}
return rootView;
}
关于主要活动我有这个:
public void onItemSelected(String id) {
Bundle arguments = new Bundle();
arguments.putString(CustomerDetailFragment.ARG_ITEM_ID, id);
CustomerDetailFragment fragment = new CustomerDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.customer_detail_container, fragment)
.commit();
}
答案 0 :(得分:3)
如果您尝试通过myLong
界面将Callbacks
值传递给DetailsFragment
,那么您的表现并不正确。回调的想法是有人实现它并将自己注册为监听器,因此当用户单击调用实现的列表项时(而不只是有一个不执行任何操作的空实现)。您可以在活动的片段之间保留通信,以显示要管理的片段。例如:
public class CustomerListFragment extends ListFragment {
private Callbacks mCallbacks;
public interface Callbacks {
public void onItemSelected(String id);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallbacks = (Callbacks) activity;
} catch (ClassCastException ex) {
Log.e(TAG, "Casting the activity as a Callbacks listener failed"
+ ex);
mCallbacks = null;
}
}
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
long myLong = values.get(position).getId();
if (mCallbacks != null) {
mCallbacks.onItemSelected(myLong);
}
}
// ...
然后在您的活动中保存两个片段:
public class MainActivity extends FragmentActivity implements CustomerListFragment.Callbacks {
// ...
public void onItemSelected(Long id) {
Bundle arguments = new Bundle();
arguments.putLong(CustomerDetailFragment.ARG_ITEM_ID, id);
CustomerDetailFragment fragment = new CustomerDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.customer_detail_container, fragment)
.commit();
}
//...
然后从传递给CustomerDetailFragment
的参数中检索id:
long theId = -1;
if (getArguments().containsKey(ARG_ITEM_ID)) {
theId = getArguments().getLong(ARG_ITEM_ID));
}
selectedCustomer = datasource.getCustomer(theId);