在使用扩展BaseAdpater的customadapter时,我有一个json在listview中显示正常。在我的测试中,我可以在没有加载到片段时读取我的json文件时完全正确地解析和扩充listview。
我现在需要移动它/将它显示在一个片段中。我的片段正在扩展ListFragment。我无法显示listfragment。
这是我片段的代码:
public class ActivitySearchResultsTwo extends ListFragment {
private ListFragment mListView;
public static ActivitySearchResultsTwo newInstance() {
ActivitySearchResultsTwo fragment = new ActivitySearchResultsTwo();
return fragment;
}
public ActivitySearchResultsTwo() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_session_row, null);
return root;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mListView = (ListView) findViewById(R.id.list);
ArrayList<Sessions> sessionsList = Sessions.getRecipesFromFile("recipes.json",getActivity());
SessionsListAdapter adapter = new SessionsListAdapter(getActivity(),sessionsList);
setListAdapter(adapter);
//getListView().setOnItemClickListener(this);
}
}
我的listview声明如下:
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
android:background="@color/colorPrimaryLight"/>
这是我的对象类
的相关摘录public class Sessions {
public static ArrayList<Sessions> getRecipesFromFile(String filename, Context context){
final ArrayList<Sessions> recipeList = new ArrayList<>();
//我解析json文件的其余部分.... //
最后我的适配器列表......
public class SessionsListAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private ArrayList<Sessions> mDataSource;
public SessionsListAdapter(Context context, ArrayList<Sessions> items) {
mContext = context;
mDataSource = items;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//1
@Override
public int getCount() {
return mDataSource.size();
}
//2
@Override
public Object getItem(int position) {
return mDataSource.get(position);
}
//3
@Override
public long getItemId(int position) {
return position;
}
//4
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// 1
if(convertView == null) {
// 2
convertView = mInflater.inflate(R.layout.fragment_session_row, parent, false);
// 3
holder = new ViewHolder();
//holder.noOfPlacesTextView = (TextView) convertView.findViewById(R.id.noOfPlaces);
holder.remainingPlacesTextView = (TextView) convertView.findViewById(R.id.remainingPlaces);
holder.activityTimeTextView = (TextView) convertView.findViewById(R.id.activityTime);
holder.activityPriceTextView = (TextView) convertView.findViewById(R.id.activityPrice);
// 4
convertView.setTag(holder);
}
else{
// 5
holder = (ViewHolder) convertView.getTag();
}
// 6
// TextView noOfPlacesTextView = holder.noOfPlacesTextView;
TextView remainingPlacesTextView = holder.remainingPlacesTextView;
TextView activityTimeTextView = holder.activityTimeTextView;
TextView activityPriceTextView = holder.activityPriceTextView;
Sessions session = (Sessions) getItem(position);
// 2
// noOfPlacesTextView.setText(session.places);
remainingPlacesTextView.setText(session.remaining + " / " + session.places + " Left");
activityTimeTextView.setText(session.eventtime);
activityPriceTextView.setText(session.price);
// 3
//Picasso.with(mContext).load(provider.imageUrl).placeholder(R.mipmap.ic_launcher).into(thumbnailImageView);
return convertView;
}
private static class ViewHolder {
public TextView remainingPlacesTextView;
public TextView activityTimeTextView;
public TextView activityPriceTextView;
}
}
任何帮助都可以让这个列表显示在片段中。 感谢
答案 0 :(得分:0)
发现问题,我没有使用列表布局更新我的片段活动中的onCreateView ...我仍然将它设置为显示我用于测试的先前布局。
View root = inflater.inflate(R.layout.fragment_session_list, container, false);
将其设置为以下解决了我的问题。
@ishmaelMakitla,感谢您在这个主题上的时间