1

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

1 回答 1

4

如果你试图通过接口将该myLong值传递给你,那你就做得不对。该回调的想法是让某人实现它并将自己注册为侦听器,因此当用户单击该实现被调用的列表项时(而不仅仅是一个不会做任何事情的空实现)。您可以将片段之间的通信留给显示要管理的片段的活动。例如:CallbacksDetailsFragment

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();
}


//...

然后从传递给的参数中检索 id CustomerDetailFragment

long theId = -1;
if (getArguments().containsKey(ARG_ITEM_ID)) {
      theId = getArguments().getLong(ARG_ITEM_ID));
}
selectedCustomer = datasource.getCustomer(theId);
于 2012-10-13T11:23:40.063 回答