Fragment中使用ListView为什么不显示出数据?

这两天刚学了Fragment,就想尝试这在fragment加入listview,可是意外的发现listview里面的数据一直显示不出来,错误的代码如下:
package com.example.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.example.testwebview.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class Wenzhang extends Fragment{
	ListView listView;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.wenzhang, container, false);
		ListView listView = (ListView)view.findViewById(R.id.article_list);
		SimpleAdapter adapter = new SimpleAdapter(this.getActivity(),getData(),R.layout.article_list_item,
				new String[]{"title","info","img"},
				new int[]{R.id.title,R.id.info});
		listView.setAdapter(adapter);
		return inflater.inflate(R.layout.wenzhang, container, false);
	}
	private List> getData() {
		List> list = new ArrayList>();

		HashMap map = new HashMap();
		map.put("title", "G1");
		map.put("info", "google 1");
		list.add(map);

		map = new HashMap();
		map.put("title", "G2");
		map.put("info", "google 2");
		list.add(map);

		map = new HashMap();
		map.put("title", "G3");
		map.put("info", "google 3");
		list.add(map);
		return list;
	}

}
在网上找了很多关于这个问题的解决方法,都说是adapter为空,可是我但不过之后发现并不是这回事,错误是这一句代码:
return inflater.inflate(R.layout.wenzhang, container, false);
我利用inflate动态加载了listview的内容,最后却没有返回父view而是直接返回res下的layout,所以正确的写法是:
return view;

你可能感兴趣的:(Fragment中使用ListView为什么不显示出数据?)