フラグメントを学習しようとしています。Eclipse で作成された Multiform プロジェクト テンプレートを使用しています。onListItemClick.
I have data loaded in a SQL database this is a list of names and addresses. でデータを渡すときに問題が発生しました。リストビューに名前がリストされており、名前をクリックしたときに名前と住所をリストしたい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();
}