2012-10-13 2 views
1

조각을 배우려고하는데 Eclipse에서 만든 Multiform 프로젝트 템플릿을 사용하고 있습니다. onListItemClick. 데이터를 전달할 때 문제가 발생합니다. SQL 데이터베이스에로드 된 데이터가 이름 및 주소 목록입니다. 목록보기에서 나는 이름이 나열되어 있고 이름이 ListView에서 클릭 될 때 이름과 주소를 나열하려고합니다. 이 작업을 수행하는 더 좋은 방법이있을 수 있지만이 작업은 처음부터 시작해야합니다.Callback onListItemClick의 데이터를 단편에 전달

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

세부 조각에는 다음과 같은 것들이 있습니다. ARG_ITEM_ID가 항상 구성 요소의 ID 값을 갖는 데이터에서 ID를 전달합니다. 당신이 DetailsFragment 당신이 그것을 바로 일을하지 않을에 Callbacks 인터페이스를 통해 그 myLong 값을 전달하려는 경우

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

: 주요 활동에

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 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 검색 : 두 조각을 보유하고 활동에 다음

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); 
    } 
} 
// ... 

그리고 예를 들어 내가 업데이트

long theId = -1; 
if (getArguments().containsKey(ARG_ITEM_ID)) { 
     theId = getArguments().getLong(ARG_ITEM_ID)); 
} 
selectedCustomer = datasource.getCustomer(theId); 
+0

을 내 위의 코드를 변경하고 onListItemClick을 샘플 코드로 변경하십시오. 그러나 여전히 CustomerDetailFragment onCreate에 ID를 가져 오는 방법을 볼 수 있습니다. 내 MainActivity는 FragmentActivity를 확장합니다. –

+0

@KimHJ 답변을 수정했습니다. 또한'CustomerListFragment' 클래스를보십시오. id를 문자열로 변환 할 필요가 없습니다. 그냥 긴 값을 사용하십시오. – Luksprog

+0

CustomerDetailFragment에 updateCustomerInfo (String Id) 프로 시저를 추가하려고 시도했지만 line datasourse.open에서 충돌했습니다. 나는 또한 id (고객 _id 및 다른 말 (id = 830022855688) –