我怎样才能更改 android 代码以使其再次工作?

How can I change the android code to make it work again?

我是一个 android 初学者,他关注 book.now 我有一个 problem.the 测试应用程序是一个新闻应用程序,并且有这样的代码:

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)

我改了,改了之后app什么都显示! 我想知道为什么我错了以及如何改变它。 非常感谢。 P.S.The class NewsAdapter extends ArrayAdapter.The class 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;
        }

}

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

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

片段文档中的示例:

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

把这个放在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()直接添加到适配器。

那么,不要在onCreateView之外重新分配adapternewsList,因为没有必要。