listfragment中的listview不起作用

时间:2013-01-08 22:06:53

标签: android android-listview android-fragments android-listfragment

我想以这种方式更改我的Android应用程序,我实现了片段。在Mainactivity中,我从xml文件中获取内容,我想在片段中填充listview。但不幸的是它不起作用,请你能帮帮我吗?这是代码。 片段布局文件:

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

这是片段文件:

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;
    }



    }




    } 

1 个答案:

答案 0 :(得分:3)

您只需在ListView中使用相应的ID:

android:id="@android:id/list"