csh_34 2013-05-08 03:45 采纳率: 0%
浏览 1977

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;
    }
    }
    } 
  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-24 17:41
    关注

    在你的 RoomFragment 类中,你已经实现了在 onCreateView 方法中获取 MainActivity 中的内容并使用它来填充 ListView。


    首先,你调用了 main() 方法来获取从 xml 文件中获取的内容。然后,你创建了一个 MyArrayAdapter 对象,并将它设置为 ListView 的适配器。最后,你在 MyArrayAdapter 类的 getView 方法中将获取的内容填充到每个列表项的 TextView 中。


    如果你想要在每个列表项中显示更多信息,例如标题和日期,你可以在 fragmentlayout.xml 中添加更多的 TextView 并在 MyArrayAdapter 的 getView 方法中设置它们的内容。


    最后,确保在 MainActivity 中将 RoomFragment 实例化并添加到你的布局中。这样,你就可以在 MainActivity 中获取内容,并在 RoomFragment 中使用 ListView 填充。

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘