我正在尝试学习片段,我正在使用在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));
}
在细节片段中,我有以下内容。即使我从数据中传递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();
}
最佳答案
如果您试图通过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);
关于android - 将回调onListItemClick上的数据传递到 fragment ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12870962/