2 csh 34 csh_34 于 2013.05.08 11:45 提问

listfragment 中的 listview 不能运行

在 Mainactivity 中我从一个 xml 文件中获取内容。然后我想在 fragment 中用 listview 填充。如何实现呢?
The fragments layout 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   <ListView
    android:id="@id/android:list"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#00FF00"
       android:drawSelectorOnTop="false" />
       <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"     
        android:id="@+id/toptext"/>  
</LinearLayout>

fragment 文件:

public class RoomFragment extends ListFragment {
    public RoomFragment() {

    }
    private MyArrayAdapter mAdapter;
    public static final String ARG_SECTION_NUMBER = "section_number";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ((Dailyquran)getActivity()).main(); // Here I am calling the method in the parent activity to get the content from the xml file. It fills "Tweets"
        Dailyquran basem = (Dailyquran)getActivity();  
        View v = inflater.inflate(R.layout.fragmentlayout, container, false);
      final ListView lv = getListView();
       mAdapter = new MyArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, basem.tweets);
       lv.setAdapter(mAdapter);
       return v;
    }
    public class MyArrayAdapter extends ArrayAdapter<Tweet>
    {
    Context mContext;
    private ArrayList<Tweet> tweets;
    Tweet data;
    public MyArrayAdapter(Context context, int textViewResourceId, ArrayList<Tweet> items) 
    {
        super(context,textViewResourceId, items);
        mContext = context;
        this.tweets = items;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        if(row == null)
        {
            LayoutInflater inflater =((Activity)mContext).getLayoutInflater();
            row = inflater.inflate(R.layout.fragmentlayout, null, false);
                TextView tt = (TextView) row.findViewById(R.id.toptext); 
        }
        else
        {
            TextView tt = (TextView) row.findViewById(R.id.toptext); 
        }
        Tweet o = tweets.get(position);  
        TextView tt = (TextView) row.findViewById(R.id.toptext);
        String survers=o.content;         
        tt.setText(survers);       
        return row;
    }
    }
    } 
Csdn user default icon
上传中...
上传图片
插入图片