我有一个使用片段的选项卡布局的应用程序,在其中一个片段中我想要一个两行/多行列表视图,我一直在关注本教程,它显示了一个ListActivity
. 我已将代码复制到我的片段中,但似乎无法让它工作。我用于片段布局的所有代码和两行代码与上面链接中的代码相同,但我想在其中显示列表的片段的 Java 类除外。
该片段的代码如下:
package com.example.shopsellswap;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
public class Fragment_My_Profile extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myProfileView = inflater.inflate(R.layout.fragment_my_profile, container, false);
return myProfileView;
}
//ArrayList holds the data (as HashMaps) to load into the ListView
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
//SimpleAdapter does the work to load the data in to the ListView
private SimpleAdapter sa;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//HashMap links each line of data to the correct TextView
HashMap<String,String> item;
for(int i=0;i<StatesAndCapitals.length;i++){
item = new HashMap<String,String>();
item.put( "line1", StatesAndCapitals[i][0]);
item.put( "line2", StatesAndCapitals[i][3]);
list.add( item );
}
sa = new SimpleAdapter(Fragment_My_Profile.this, list,
R.layout.my_two_lines,
new String[] { "line1","line2" },
new int[] {R.id.line_a, R.id.line_b});
setListAdapter(sa);
}
private String[][] StatesAndCapitals =
{{"Alabama","Montgomery"},
{"Alaska","Juneau"},
{"Arizona","Phoenix"},
{"Arkansas","Little Rock"},
{"California","Sacramento"}};
给我错误的部分是
sa = new SimpleAdapter(Fragment_My_Profile.this, list,
R.layout.my_two_lines,
new String[] { "line1","line2" },
new int[] {R.id.line_a, R.id.line_b});
setListAdapter(sa);
具体错误是:
The constructor SimpleAdapter(Fragment_My_Profile, ArrayList<HashMap<String,String>>, int, String[], int[]) is undefined
奇怪的是当我更改ListFragment
为ListActivity
错误不再存在时
为什么它不起作用,我该如何解决?