2012-11-10 3 views
1

하나의 탭에 4 개의 탭이있는 조각이 있습니다. 목록보기를 표시해야합니다. 나는이 코드를 멀리 가지고 있지만 몇 가지 오류가 있고 내가 제대로하지 않거나 간단한 것을 놓친다면 확실하지 않다. 여기에 내가이안드로이드의 조각에 이미지가있는 목록보기를 표시하는 방법

SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to); 

ListView listView = (ListView) findViewById(R.id.listview); 

에서 이러한 코드 두 줄에 오류를 얻고있다

public class SupportFragment extends SherlockFragment{ 
String[] supporters = new String[] { 
     "Gulf Shores", 
     "Central", 
     "Spanish Fort", 
     "Jackson Heights", 
     "Summerdale", 
     "Atlas", 
     "Robertsdale", 
     "Eastern Shore" 
}; 

int[] images = new int[] { 
     R.drawable.gulfshores, 
     R.drawable.central, 
     R.drawable.spanishfort, 
     R.drawable.jacksonheights, 
     R.drawable.summerdale, 
     R.drawable.atlas, 
     R.drawable.robertsdale, 
     R.drawable.easternshore 
}; 

String[] church = new String[]{ 
     "Chruch of Christ", 
     "Chruch of Christ", 
     "Chruch of Christ", 
     "Chruch of Christ", 
     "Chruch of Christ", 
     "Chruch of Christ", 
     "Chruch of Christ", 
     "Chruch of Christ" 
}; 


@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 

    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.tab_support_layout, null); 


    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>(); 

     for(int i=0;i<10;i++){ 
      HashMap<String, String> hm = new HashMap<String,String>(); 
      hm.put("sup", "Supporters : " + supporters[i]); 
      hm.put("chur","Church : " + church[i]); 
      hm.put("icon", Integer.toString(images[i])); 
      aList.add(hm); 
     } 

     String[] from = {"sup", "chur", "icon"}; 

     int[] to = {R.id.icon, R.id.sup, R.id.chur}; 

     SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to); 

     ListView listView = (ListView) findViewById(R.id.listview); 

     listView.setAdapter(adapter); 


     return root; 

} 



} 

조각 활동을 사용하고 내 코드입니다 내가 얻는 오류는 다음과 같습니다.

있어서 getBaseContext()가 입력 SupportFragment

있어서 findViewById를 (INT)에 대해 정의되지는 입력 SupportFragment리스트 뷰

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:background="@drawable/tableback" > 

<TextView 
    android:id="@+id/textview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

<ListView 
    android:id="@+id/listview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 
</LinearLayout> 

위한

XML 및리스트 뷰 레이아웃 정의되지

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal"> 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="10dp" 
    android:paddingRight="10dp" 
    android:paddingBottom="10dp" 
/> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
> 
    <TextView 
     android:id="@+id/sup" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="15dp" 
    /> 

    <TextView 
     android:id="@+id/chur" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="10dp" 
    /> 
</LinearLayout> 
</LinearLayout> 

도움을 주시면 큰 도움이됩니다.

답변

2

을 가지고 있지 않으므로 을 가지고 있으므로 getBaseContext()으로 컨텍스트를 가져올 수 없습니다. 대신 getActivity()을 사용해야합니다. Fragment에서 원하는 경우보기를 확대하여 findViewById을 사용해야합니다. 그러나 탭을 사용하려면 이있는 FragmentActivity이 필요합니다.

당신은 당신의 조각에 onCreateView() 방법이 같은보기를 부풀려 수

: 내가 조각 활동 말했다 왜

[...]view.findViewById(...); 
+0

오른쪽 내가 모르는 :

View view = inflater.inflate(R.layout.myLayout, null); 

그런 다음이 같은 findViewById를 호출 할 수 있습니다 getActivity()는 오류를 해결하기 위해 노력했지만 findViewById를 사용하려면 뷰를 부 풀릴 때 100 % 확신하지 못합니다. 이 작업을 수행하는 데 사용하는 좋은 방법인지 확실하지 않습니다. 그것에 대한 생각을 가지고 있거나 뷰를 부 풀릴 수있는 방향으로 나를 가리킬 수 있습니까? – Bryan

+0

모든 조각을 처리하는 조각 활동이 있습니다. 이것은 처리하는 조각 중 하나입니다. – Bryan

+0

@Bryan이 게시물을 편집했습니다. – Ahmad

관련 문제