ListFragment似乎没有膨胀

时间:2014-04-30 23:10:16

标签: java android

希望有人可以提供帮助。我遇到了一个问题似乎在夸大我的看法。我的目标是使用自定义数组适配器在自定义布局中显示项目列表。我使用另一个SO帖子重新编写了一些原始代码,但是我的简单适配器工作正常,新列表不会显示。请参阅以下内容:

这是我的Fragment类。

HomeFeedFragment.java

    public class HomeFeedFragment extends ListFragment {

    private ListView listView;
    private ArrayList<MainEvent> items;
    private MainEventListViewAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_homefeed,
                container, false);
        listView = (ListView) rootView.findViewById(android.R.id.list);

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //GetEvents is just a test method to return a list of pre-filled event objects
        items = GlobalList.GetEvents();
        adapter = new MainEventListViewAdapter(getActivity(), android.R.id.list, items);
        listView.setAdapter(adapter);
    }


      @Override
      public void onListItemClick(ListView l, View v, int position, long id) {
        // do something with the data
      }

}

我的自定义行布局xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!-- ListRow Left sied Thumbnail image -->

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:padding="3dip" >

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip" />
    </LinearLayout>

    <!-- Heading Text -->

    <TextView
        android:id="@+id/eventheader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="#040404"
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="sans" />

    <!-- Event Description -->

    <TextView
        android:id="@+id/eventdescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/thumbnail"
        android:layout_below="@id/eventheader"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="#343434"
        android:textSize="10sp"
        tools:ignore="SmallSp" />

    <!-- Posted Time -->

    <TextView
        android:id="@+id/eventpostedtime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/eventheader"
        android:layout_marginRight="5dip"
        android:gravity="right"
        android:textColor="#10bcc9"
        android:textSize="10sp"
        android:textStyle="bold"
        tools:ignore="SmallSp" />

</RelativeLayout>

实际的片段布局:

    <?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" >    

    <ListView
      android:id="@android:id/list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
    </ListView> 


</LinearLayout>

我的自定义ArrayAdapter:

public class MainEventListViewAdapter extends ArrayAdapter<MainEvent> {
    private Context context;
    private List<MainEvent> eventList;

    public MainEventListViewAdapter(Context context, int textViewResourceId, List<MainEvent> eventList){
        super(context, textViewResourceId, eventList);
        this.context = context;
        //this.eventList = eventList;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
        TextView txtEventHeader;
        TextView txtEventDescription;
        TextView txtEventPostedTime;
    }

    public int getCount(){
        if(eventList!=null){
            return eventList.size();
        }
        return 0;
    }

    public MainEvent getItem(int position){
        if(eventList!=null){
            return eventList.get(position);
        }
        return null;
    }

    public long getItemId(int position){
        if(eventList!=null){
            return eventList.get(position).hashCode();
        }
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;
        MainEvent rowItem = getItem(position);

        LayoutInflater  inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if(convertView==null){
            convertView = inflater.inflate(R.layout.fragment_homefeed_rowlayout, parent, false);
            holder = new ViewHolder();
            holder.txtEventHeader = (TextView) convertView.findViewById(R.id.eventheader);
            holder.txtEventDescription = (TextView) convertView.findViewById(R.id.eventdescription);
            holder.txtEventPostedTime = (TextView) convertView.findViewById(R.id.eventpostedtime);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtEventHeader.setText(rowItem.getHeadline());
        holder.txtEventDescription.setText(rowItem.getDescription());
        holder.txtEventPostedTime.setText(rowItem.getPostedTime());

        /*TextView text = (TextView) v.findViewById(R.id.label);
        text.setText(m.getHeadline());
        ImageView imageView = (ImageView) v.findViewById(R.id.icon);
        imageView.setTag(m.getImageURL());
        //DownloadImageTask dt = new DownloadImageTask();
        dt.execute(imageView);

        return v;*/
        return convertView;
    }

    public List<MainEvent> getEventList(){
        return eventList;
    }

    public void setEventList(List<MainEvent> eventList){
        this.eventList = eventList;
    }

}

^我最初在这个类中有一个异步任务来检索每个项目的图像,但我现在已经删除了所有这些。

我没有在LogCat中发现任何实际错误,没有崩溃或任何事情,View只是没有膨胀。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

这很令人尴尬。我发现了这个问题。显然,我忘记取消注释初始化事件列表的适配器构造函数中的行:

//this.eventList = eventList;

我暂时评论它以修复另一个错误。现在工作正常,但感谢你的建议。