如何更改Android代码以使其再次工作?

时间:2016-11-16 14:39:05

标签: android android-fragments fragment-lifecycle

我是一个关注一本书的机器人初学者。现在我有一个问题。测试应用程序是一个新闻应用程序,并有这样的代码:

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)

我改变它,但应用程序在更改后显示任何内容! 我想知道为什么我错了以及如何改变它。 非常感谢。 P.S。类NewsAdapter扩展了ArrayAdapter。类新闻只有两个字符串 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;
        }

}

2 个答案:

答案 0 :(得分:0)

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

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

Fragment文档中的示例:

@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;
}

答案 1 :(得分: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()以直接添加到适配器。

然后,不要在adapter之外重新分配newsListonCreateView,因为没有必要。