0

我是一个遵循一本书的android初学者。现在我遇到了一个问题。测试应用程序是一个新闻应用程序,代码如下:

public class NewsTitleFragment extends Fragment implements OnItemClickListener {
    private ListView newsTitleListView;
    private List<News> newsList;
    private NewsAdapter adapter;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        newsList = getNews();
        adapter = new NewsAdapter((Activity)activity, R.layout.news_item, newsList);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater
            .inflate(R.layout.news_title_frag, container, false);
        newsTitleListView = (ListView) view
            .findViewById(R.id.news_title_list_view);
        newsTitleListView.setAdapter(adapter);
        newsTitleListView.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getActivity().findViewById(R.id.news_content_layout) != null) {
            isTwoPane = true;
        } else {
            isTwoPane = false;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
        News news = newsList.get(position);
        if (isTwoPane) {
            NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager()
                .findFragmentById(R.id.news_content_fragment);
            newsContentFragment.refresh(news.getTitle(), news.getContent());
        } else {
            NewsContentActivity.actionStart(getActivity(), news.getTitle(),
                news.getContent());
        }
    }

    private List<News> getNews() {
        List<News> newsList = new ArrayList<News>();
        News news1 = new News();
        news1.setTitle("It's news 1");
        news1.setContent("It's news 1 content");
        newsList.add(news1);
        News news2 = new News();
        news2.setTitle("It's news 2");
        news2.setContent("It's news 2 content");
        newsList.add(news2);
        return newsList;
    }

}

但在新的 android API 中,代码

public void onAttach(Activity activity)

必须改为

public void onAttach(Context context)

我更改了它,但应用程序在更改后显示任何内容!我想知道为什么我错了以及如何改变它。非常感谢。PSNewsAdapter 类扩展了 ArrayAdapter。News 类只有两个字符串 NewsAdapter 方法是:

public class NewsAdapter extends ArrayAdapter<News> {

    private int resourceId;

    public NewsAdapter(Context context, int textViewResourceId, List<News> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        News news = getItem(position);
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
        } else {
            view = convertView;
}
    TextView newsTitleText = (TextView) view.findViewById(R.id.news_title);
newsTitleText.setText(news.getTitle());
        return view;
        }

}
4

2 回答 2

0

把这个放进去onCreateView

newsList = new ArrayList<News>();
adapter = new NewsAdapter(getActivity(), R.layout.news_item, newsList);
newsTitleListView.setAdapter(adapter);

而不是newsList = getNews(),使用

adapter.clear(); // or newsList.clear();
adapter.addAll(getNews()); // or newsList.addAll();
/** ... but, if using alternatives **/
// adapter.notifyDataSetChanged(); // this is needed if not using adapter methods directly

或者,重新编写getNews()直接添加到适配器。

然后,不要重新分配adapterornewsList外部,onCreateView因为没有必要。

于 2016-11-17T02:16:47.957 回答
0

请查看 Android 片段文档https://developer.android.com/reference/android/app/Fragment.html

正如 cricket_007 已经提到的,您必须在 onCreateView 方法中设置或创建布局并返回它。

片段文档中的示例:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    ScrollView scroller = new ScrollView(getActivity());
    TextView text = new TextView(getActivity());
    int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            4, getActivity().getResources().getDisplayMetrics());
    text.setPadding(padding, padding, padding, padding);
    scroller.addView(text);
    text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
    return scroller;
}

或者,如果您已经有一个 XML 布局,您可以简单地在 onCreateView 方法中对它们进行扩充:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = (LinearLayout) inflater.inflate(R.layout.fragment_layout, container, false);
        return view;
}
于 2016-11-16T14:55:36.267 回答