我有一个包含自定义列表视图的活动。
它有一个自定义数组适配器(作为另一个 java 文件)和一个 setter 和 getter 类(作为另一个 java 文件)。我想将此自定义列表视图放入片段中,但总是出错。这是我之前的主要活动代码: public class MainActivity extends Activity implements OnClickListener {
private ListView listView;
private List<NameBean> items;
private NamesAdapter objAdapter = null;
private Button btnGetSelected;
public static final String[] titles = new String[] { "Strawberry",
"Banana", "Orange", "Mixed" };
String CheckedItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.spinner_array, android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerAdapter.setNotifyOnChange(true);
listView = (ListView) findViewById(R.id.listview);
btnGetSelected = (Button) findViewById(R.id.btnget);
btnGetSelected.setOnClickListener(this);
items = new ArrayList<NameBean>();
for (int i = 0; i < titles.length; i++) {
NameBean nb = new NameBean();
nb.setName(titles [i]);
nb.setSpinnerAdapter(spinnerAdapter);
items.add(nb);
}
objAdapter = new NamesAdapter(MainActivity.this, items);
listView.setAdapter(objAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
...
}
});
}
@Override
public void onClick(View v) {
...
}
}
}
}
这就是我想在 Fragment 中使用它的方式:
公共类内容扩展片段{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, container, false);
return view;
}
public void setThings(ListAdapter adapter) {
ListView listView = (ListView) getView().findViewById(R.id.listview);
listView.setAdapter(adapter);
}
}
我在另一个片段中定义了这些参数(在同一个活动中):
public class MyListFragment extends Fragment {
private ListView listView;
private List<NameBean> items;
private NamesAdapter objAdapter = null;
public static final String[] titles = new String[] { "Strawberry",
"Banana", "Orange", "Mixed" };
String CheckedItems;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_items,
container, false);
Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
updateDetail();
}
});
return view;
}
// May also be triggered from the Activity
public void updateDetail() {
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(),
R.array.spinner_array, android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerAdapter.setNotifyOnChange(true);
items = new ArrayList<NameBean>();
for (int i = 0; i < titles.length; i++) {
NameBean nb = new NameBean();
nb.setName(titles [i]);
nb.setSpinnerAdapter(spinnerAdapter);
items.add(nb);
}
objAdapter = new NamesAdapter(getActivity(), items);
Content fragment = (Content) getFragmentManager()
.findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setThings (objAdapter);
}
}
}
我不知道问题是什么。请帮忙。