sinat_35054086
0kangkang0
2016-10-17 11:39

android 中 fragment 嵌入listview

首先在MainActivity中

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

然后在主布局activity_main中只有一个fragment

        <fragment 
        android:id="@+id/title_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.news.TitleFragment"
        />

在com.example.news.TitleFragment里面
我定义了listview ,和自己定义的newsadapter适配器,
还有要装入listview中的数据data。

代码如下:

public class TitleFragment extends Fragment implements OnItemClickListener {
    private NewsAdapter adapter;
    private List<News> data;
    private ListView listView;
    private boolean isTwoPane;
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        adapter = new NewsAdapter(activity, R.layout.title_list_item, data);
        data = getNews();
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Log.i("test", "onCreateView");
        View view = inflater.inflate(R.layout.title_frag, container, false);
        listView = (ListView) view.findViewById(R.id.listView1);
        listView.setAdapter(adapter);
        // listView.setOnItemClickListener(this);
        return view;
    }

    private List<News> getNews() {
        List<News> data = new ArrayList<News>();
        News news1 = new News();
        news1.setTitle("chongqing is beautiful city!!!");
        news1.setContext("There have everything in ChongQing,delicious food ,great building and hot girls ");
        data.add(news1);

        News news2 = new News();
        news2.setTitle("chongqing is beautiful city2222!!!");
        news2.setContext("There have everything in ChongQing,delicious food ,great building and hot girls ");
        data.add(news2);
        return data;

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub

    }
}

在newsadapter适配器:
我试过不用自己定义的适配器就可以,但是用自己定义的适配器就会出错,但是我不知道到底错误在哪。

代码如下:

 public class NewsAdapter extends ArrayAdapter<News> {
    private int resourceId;
    public NewsAdapter(Context context, int textViewResourceId, List<News> objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
        resourceId = textViewResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        News news = getItem(position);
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);

        } else {
            view = convertView;
        }
        TextView titleText = (TextView) view.findViewById(R.id.title);
        titleText.setText(news.getTitle());
        return view;
    }
}

R.layout.title_list_item代码:

 <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TextView>

最后就出现binary xml file line #11:error inflating class fragment 错误。

本人安卓小白一名,求大神告诉我到底哪里错了呀。跪谢

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答