Hy,我是android的新手,我正在尝试在片段中构建ListView。我有一个主类MainActivity extends FragmentActivity implements ActionBar.TabListener
和一些内部类。
我使用public class PopulatePageAdapter extends FragmentPagerAdapter
来返回我需要的片段,以及一个代表我片段的类:
public class usersList extends Fragment {
private List<User> usersList = new ArrayList<User>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list, container, false);
populateUsernameList();
ArrayAdapter<User> adapter = new MyListAdapter();
ListView list = (ListView)rootView.findViewById(R.id.listView);
Log.d("Q","QQ");
list.setAdapter(adapter);//here my app fails
Log.d("Q","WW");
return rootView;
}
这是我的ArrayAdapter
private class MyListAdapter extends ArrayAdapter<User> {
public MyListAdapter() {
super(getActivity().getApplicationContext(), R.layout.contact_layout, usersList);
}
public View getView(int position, View convertView, ViewGroup parent) {
View userView = convertView;
if (userView == null) {
userView = MainMenuActivity.this.getLayoutInflater().inflate(R.layout.contact_layout, parent, false);
}
User user = usersList.get(position);
ImageView imageView = (ImageView) userView.findViewById(R.id.icon);
imageView.setImageResource(user.getProfilePictureId());
TextView textView = (TextView) userView.findViewById(R.id.textViewCompleteName);
textView.setText(user.getFirstName() + " " + user.getLastName());
return userView;
}
}
我在想我没有正确使用ArrayAdapter,但我没有看到任何其他方式使用它。
答案 0 :(得分:0)
看起来您不会将数据(userList)解析为适配器。也许看看这个:http://www.vogella.com/tutorials/AndroidListView/article.html
您必须将数据附加到适配器并解析您的上下文:
ArrayList<User> usersList;
Context mContext;
public MyListAdapter(Context context, ArrayList<User> myListViewData) {
this.mContext = context;
this.usersList = myListViewData;
}
答案 1 :(得分:0)
您需要将数据传递到适配器
ArrayList<Data> data=new ArrayList<Data>();
data= getArguments().getParcelableArrayList("data");
adapter = new DataAdapter(getActivity(),data);
listView.setAdapter(adapter);