我正在尝试学习片段,我正在使用在 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();
}