这是我的片段页面,我正在尝试创建列表视图
public class MessagesFragment extends Fragment {
ArrayList<String> brands= new ArrayList<String>();
//String[] brands = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
public MessagesFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_messages, container, false);
brands.add("microsoft");
brands.add("ios" );
brands.add("android");
AdapterListView adapter = new AdapterListView(brands,getActivity());
ListView listView = (ListView)rootView.findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_messages, container, false);
}
}
这是我的适配器类
public class AdapterListView extends BaseAdapter {
Context context;
ArrayList<String> brands;
public AdapterListView(ArrayList<String> brands, Context context) {
this.brands = brands;
this.context = context;
}
@Override
public int getCount() {
return brands.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view= LayoutInflater.from(context).inflate(R.layout.activity_listview,null);
ListView lv=(ListView)view.findViewById(R.id.mobile_list);
lv.getAdapter();
return view;
}
}
我需要设置列表品牌..目前我没有获得列表品牌的任何价值。我认为错误是适配器类的最后一部分
答案 0 :(得分:5)
return rootView;
的onCreateView
方法中的MessagesFragment
而不是
return inflater.inflate(R.layout.fragment_messages, container, false);
答案 1 :(得分:1)
正如@Rustam在答案中所说,你需要从那里返回rootView
。
我认为你的适配器类的getview()
应该有更多的变化为..
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view= LayoutInflater.from(context).inflate(R.layout.activity_listview,null);
// layout.activity_listview should be your custom row layout with textview
// find the TextView of your custom row and set data on it,,
TextView tv = (TextView) view.findViewById(R.id.yourTextview);
tv.setText(brands.get(i));
return view;
}