我想以这种方式更改我的 android 应用程序,即我实现片段。在 Mainactivity 中,我从 xml 文件中获取内容,我想用片段中的列表视图填充该内容。但不幸的是它不起作用,请你能帮助我吗?这是代码。片段布局文件:
<?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;
}
}
}