I have a fragment with 4 tabs, in one tab i need to show a listview. I have got this far on my code but have a couple errors and i’m not sure if i’m just not doing it right or if i’m missing something simple. Here is my code that i’m using
fragment activity
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;
}
}
I am getting an error on these two lines of code from this
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
and
ListView listView = (ListView) findViewById(R.id.listview);
the error i’m getting is:
The method getBaseContext() is undefined for the type SupportFragment
The method findViewById(int) is undefined for the type SupportFragment
xml for listview
<?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>
and the listview layout
<?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>
any help you can give is greatly appreciated
You do not have a
FragmentActivityyou have aFragment, so of course you can’t get the Context withgetBaseContext(), you have to usegetActivity()instead.You also have to inflate a View to use
findViewById, if you want to do that in a Fragment. However if you want to use Tabs you need aFragmentActivitywhich hadels yourFragments.You can inflate a View like this in your fragments
onCreateView()method:Then you can call
findViewByIdlike this: