|
[技术讨论]
安卓Fragment在调用自定义ArrayAdapter时的问题
1#
发表于 2016-5-31 11:51:24
|
查看: 3094 |
回复: 3
我显示自定义了一个类继承Fragment:+提示: -- package com.myFragment;
- import java.util.ArrayList;
- import java.util.List;
- import com.example.yuji_project_mcy.R;
- import com.example.yuji_project_mcy.array_adapter.NoteAdapter;
- import com.example.yuji_project_myc.listclass.NoteList;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ListView;
- public class Fragment_of_note extends Fragment {
- private List<NoteList> notelist_text_zxzt = new ArrayList<NoteList>();
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.note_text, container, false);
- initNoteList_text_zxzt();
- NoteAdapter noteadapter = new NoteAdapter(getActivity(),
- R.layout.note_text, notelist_text_zxzt);
- ListView listView = (ListView) view.findViewById(R.id.note_text_zxzt);
- listView.setAdapter(noteadapter);
- return view;
- }
- private void initNoteList_text_zxzt() {
- NoteList itme1 = new NoteList("既然收不到勋章,那就收纸娃娃吧", "雨季空灵",R.drawable.avatar,
- false, false, false, false, false, false,
- false);
- notelist_text_zxzt.add(itme1);
- NoteList itme2 = new NoteList("测试测试测试", "雨季", R.drawable.avatar, true,
- false, false, true, false, false, false);
- notelist_text_zxzt.add(itme2);
- }
- }
复制代码
我的fragment内添加的是一个自定义的ListView的控件,下面给出我调用fragment的主界面布局和frament使用的布局以及listview每一条itme的布局:
+提示: 包含自定义listview的布局-- <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="#66CCFF" >
- <com.myListView.MyListView
- android:id="@+id/note_text_zxzt"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- </com.myListView.MyListView>
- </LinearLayout>
复制代码
下面给出我自定义listview的代码:
+提示: 自定义的ListView-- package com.myListView;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.widget.ListView;
- public class MyListView extends ListView {
- public MyListView(Context context) {
- super(context);
- }
- public MyListView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public MyListView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
- MeasureSpec.AT_MOST);
- super.onMeasure(widthMeasureSpec, expandSpec);
- }
- }
复制代码
最后就是自定义适配器了:
对了,还有一个自定义的类,用来放item的ID,名称之类的:+提示: -- package com.example.yuji_project_myc.listclass;
- public class NoteList {
- private String post_name;
- private String poster_name;
- private int post_imageId;
- private boolean tag_qzdz;
- private boolean tag_bbdz;
- private boolean tag_dttg;
- private boolean tag_rmt;
- private boolean tag_htjl;
- private boolean tag_jf;
- private boolean tag_fj;
- public NoteList(String post_name, String poster_name, int post_imageId,
- boolean tag_qzdz, boolean tag_bbdz, boolean tag_dttg,
- boolean tag_rmt, boolean tag_htjl, boolean tag_jf, boolean tag_fj) {
- this.post_name = post_name;
- this.poster_name = poster_name;
- this.post_imageId = post_imageId;
- this.tag_qzdz = tag_qzdz;
- this.tag_bbdz = tag_bbdz;
- this.tag_dttg = tag_dttg;
- this.tag_rmt = tag_rmt;
- this.tag_htjl = tag_htjl;
- this.tag_jf = tag_jf;
- this.tag_fj = tag_fj;
- }
- public String getPost_name() {
- return post_name;
- }
-
- public String getPoster_name() {
- return poster_name;
- }
-
- public int getPost_imageId() {
- return post_imageId;
- }
- public boolean isTag_qzdz() {
- return tag_qzdz;
- }
- public boolean isTag_bbdz() {
- return tag_bbdz;
- }
- public boolean isTag_dttg() {
- return tag_dttg;
- }
- public boolean isTag_rmt() {
- return tag_rmt;
- }
- public boolean isTag_htjl() {
- return tag_htjl;
- }
- public boolean isTag_jf() {
- return tag_jf;
- }
- public boolean isTag_fj() {
- return tag_fj;
- }
- }
复制代码
然后DEBUG时到NoteAdapter的
viewHolder.Post_Image.setImageResource(notelist.getPost_imageId());
viewHolder.Post_Name.setText(notelist.getPost_name());
这里就开始出错,不知道为什么,请问是怎么回事?
|
|