![](/img/trans.png)
[英]How to make List of button using arrayAdapter and Listfragment in android?
[英]Android: Use different layouts in ListFragment using ArrayAdapter
大家好,谢谢您阅读我的问题!
我目前正在开发具有以下层次结构的android应用程序:一个FragmentActivity
除以三个ListFragments
。 其中一个的ListFragments
使用自定义ArrayAdapter
显示List<>
在一个XML模板ListView
此List<>
填充有被赋予ID的数据集。 ID小于10的数据集被标记为“重要”,其他数据集被标记为“不太重要”
这就是我要寻找的:我有两个布局模板,并且我想用一种布局显示“重要”数据集,而用另一种布局显示“不太重要”的数据集。
我已经搜索了很长时间,但似乎以前没有人问过这样的问题。 我尝试使用第二个ListView
并在其上应用第二个自定义ArrayAdapter
,但是很遗憾,存在一个限制:由于我使用的是ListFragment
,因此我不得不像这样使用findViewById
。
ListView list;
list = (ListView) rootView.findViewById(android.R.id.list);
当我尝试编写第二个自定义ArrayAdapter
,它在此处显示错误:
ListView list;
list_flat = (ListView) rootView.findViewById(android.R.id.list_flat);
这是整个ListFragment
public class LeaguePage extends ListFragment{
Global global_var;
ListView list, list_flat;
List <League> leagues = null;
ListAdapter adapter = null;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.league_page, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
list_flat = (ListView) rootView.findViewById(android.R.id.list_flat);
try {
leagues = Leagues_Parser.parse(getActivity().getAssets().open("league_raw.xml"));
} catch (IOException e) {
e.printStackTrace();
}
adapter = new LeagueAdapter (getActivity(), R.layout.list_row, leagues);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Global.selectedTeam = Integer.valueOf(leagues.get(arg2).getInternalID());
Global.mViewPager.setCurrentItem(1, true);
}
});
}
}
这是我传递给 ArrayAdapter
的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/liga_flat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:paddingBottom ="10dip"
android:textColor="#040404"
android:textSize="25sp"
android:textStyle="normal"
android:typeface="serif" />
</RelativeLayout>
这是 ListFragment
的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="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main_Activity$search_page" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main_Activity$search_page" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="0dp"
android:divider="@null">
</ListView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main_Activity$search_page" >
<ListView
android:id="@+id/android:list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:dividerHeight="0dp" >
</ListView>
</RelativeLayout>
</RelativeLayout>
您不应该给ListView
的ID: android:id="@+id/android:list2"
,您的ListView
的ID应该是
<ListView android:id="@id/android:list"
...... ></ListView>
要么
<ListView android:id="@+id/sampleList"
...... ></ListView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.