2013-11-14 3 views
1

나는 안드로이드 개발의 초보자입니다. 프로젝트의 경우 조각에 listview를 구현하는 방법을 알고 싶습니다.조각에 listview를 구현하는 방법은 무엇입니까?

내 프로젝트에는 3 개의 조각이 있습니다.

내 휴대 전화에있는 모든 연락처를 검색해야합니다. (이 예를 사용하려면 =>http://samir-mangroliya.blogspot.fr/p/android-read-contact-and-display-in.html). 그런 다음 목록 뷰가 포함 된 조각으로 하나의 조각에 넣을 것입니다.

많은 예제를 찾고 있지만 ACTIVITY 예제의 경우에만 listview입니다.

도와주세요.

안부,

Tofuw

PS :/

편집 여기

내 코드입니다 : 내 나쁜 영어 죄송합니다, 나는 프랑스어 해요

public class PhoneFrag extends ListFragment 
{ 
private List<Contact>listecontact=new ArrayList<Contact>(); 

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
} 

public void onActivityCreated(Bundle savedInstanceState, Context context) 
{ 
    super.onActivityCreated(savedInstanceState); 

    String[]values=new String[]{}; 

    Cursor phones=context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null); 

    while(phones.moveToNext()) 
    { 
     String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
     String num=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

     Contact contact=new Contact(); 
     contact.setName(name); 
     contact.setNum(num); 

     listecontact.add(contact); 
    } 
    phones.close(); 

    ArrayAdapter<String>adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_2, listecontact); 
    setListAdapter(adapter); 
} 

오류가 있습니다. 세 번째 마지막 줄 :

생성자 ArrayAdapter와 (FragmentActivity, INT, 목록)

당신이 날 도와 줘요 수 정의되지?

는 Tofuw는

+2

왜 ListFragment를 사용하지 않는가? http : // stackoverflow.com/questions/6510550/android-listview-in-fragment – Neil

+0

Activty와 동일합니다. XML을 빌드하십시오. ListView lv = (ListView) fragmentRootView.findViewById (R.id.your_list_view);를 사용하여 onCreateView (...) 내에서 ListView 인스턴스 가져 오기 '내 문제가 보이지 않는다. –

+0

안녕하십니까. ListFragment를 사용하고 있습니다 :) – Tofuw

답변

3

당신이 사용되거나 더 이상 setContentView(layout)를 사용하여 XML 레이아웃 파일이 조각을 연결할 필요가 없습니다 ListActivity와 예제를 보았다 .If 당신의 조각 대신 FragmentListFragment을 연장 받기, 안드로이드는 기성품을 생성

: setListAdapter(adapter)처럼 conveniance.Instead의 conveniance 방법에 대한 그 안에 ListViewFragment는 경우에 당신이 ListView 인스턴스가 getListView() 국지적 인 조각이 할 onActivityCreated를 사용하는 것 사용으로 접근하고 싶습니다, you.However을 사용할 수 있습니다

ListActivitygetActivity()ListFragment에있는 것으로 이해할만큼 조각을 잘 알고 있다고 생각합니다.

public static class TitlesFragment extends ListFragment { 
boolean mDualPane; 
int mCurCheckPosition = 0; 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    // Populate list with our static array of titles. 
    setListAdapter(new ArrayAdapter<String>(getActivity(), 
      android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES)); 

    // Check to see if we have a frame in which to embed the details 
    // fragment directly in the containing UI. 
    View detailsFrame = getActivity().findViewById(R.id.details); 
    mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE; 

    if (savedInstanceState != null) { 
     // Restore last state for checked position. 
     mCurCheckPosition = savedInstanceState.getInt("curChoice", 0); 
    } 

    if (mDualPane) { 
     // In dual-pane mode, the list view highlights the selected item. 
     getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     // Make sure our UI is in the correct state. 
     showDetails(mCurCheckPosition); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putInt("curChoice", mCurCheckPosition); 
} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    showDetails(position); 
} 

/** 
* Helper function to show the details of a selected item, either by 
* displaying a fragment in-place in the current UI, or starting a 
* whole new activity in which it is displayed. 
*/ 
void showDetails(int index) { 
    mCurCheckPosition = index; 

    if (mDualPane) { 
     // We can display everything in-place with fragments, so update 
     // the list to highlight the selected item and show the data. 
     getListView().setItemChecked(index, true); 

     // Check what fragment is currently shown, replace if needed. 
     DetailsFragment details = (DetailsFragment) 
       getFragmentManager().findFragmentById(R.id.details); 
     if (details == null || details.getShownIndex() != index) { 
      // Make new fragment to show this selection. 
      details = DetailsFragment.newInstance(index); 

      // Execute a transaction, replacing any existing fragment 
      // with this one inside the frame. 
      FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      if (index == 0) { 
       ft.replace(R.id.details, details); 
      } else { 
       ft.replace(R.id.a_item, details); 
      } 
      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
      ft.commit(); 
     } 

    } else { 
     // Otherwise we need to launch a new activity to display 
     // the dialog fragment with selected text. 
     Intent intent = new Intent(); 
     intent.setClass(getActivity(), DetailsActivity.class); 
     intent.putExtra("index", index); 
     startActivity(intent); 
    } 
} 

}

또한 커서에 행이없는 경우에 메시지를 표시하고 전환하는 setEmptyTextsetListShown 같은 방법을 사용할 수 있습니다 여기에

내가 Android Fragment Documentation에있는 코드 또한 각각의 안드로이드의 로더 프레임 워크는 Fragment에서만 지원되며 지원 라이브러리에는 Activity가 없습니다.

+0

답장 @ user2309862에 감사드립니다. 하지만 지금은 다른 문제가 있습니다. 도와 줄수있으세요 ? 아래에서 코드를 게시하고 세부 정보를 더 자세하게 게시합니다. – Tofuw

관련 문제