我创建了一个android操作栏选项卡应用程序,有4个选项卡,其中一个选项卡是 片段类,用于包含列表视图,以显示静态信息列表:
但是,我在这行代码中遇到错误: SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),aList,
R.layout.listoflockers, from, to);
这些是用于创建列表视图的2 xml文件:
listoflockers.xml
<?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" >
<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>
lockerinforow.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/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/email"
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/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
/>
<TextView
android:id="@+id/cur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
/>
<TextView
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
/>
</LinearLayout>
</LinearLayout>
public class myLockerFragment.java{
.....
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mylocker, container, false);
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("txt", "Locker No : " + i);
hm.put("cur","Location : " + currency[i]);
hm.put("size","Size : " + size[i]);
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur","size" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur,R.id.size};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listoflockers, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) rootView.findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
return rootView;
}
请帮忙 我对这一行很困惑:
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listoflockers, from, to);
给我错误: 对于myLockerFragment类型
,方法getBaseContext()未定义答案 0 :(得分:3)
您可以在getActivity()
中使用Fragment
来获取包含片段本身的上下文
答案 1 :(得分:0)
这样做 -
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList,
R.layout.listoflockers, from, to);
答案 2 :(得分:0)
SimpleAdapter adapter = new SimpleAdapter(getActivity().getApplicationContext(), aList, R.layout.listoflockeres, from, to);
您需要在getActivity()
Fragments
方法
答案 3 :(得分:0)
除了使用getBaseContext()
之外,您还需要使用getActivity()
中的Fragments
来获取片段的上下文。
试试如下:
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList,R.layout.listoflockers, from, to);