简体   繁体   English

Android:使用ArrayAdapter在ListFragment中使用不同的布局

[英]Android: Use different layouts in ListFragment using ArrayAdapter

Hello everyone and thank you for reading my question! 大家好,谢谢您阅读我的问题!

I'm currently developing an android app that has the following hierarchy: One FragmentActivity is divided by three ListFragments . 我目前正在开发具有以下层次结构的android应用程序:一个FragmentActivity除以三个ListFragments One of these ListFragments uses a custom ArrayAdapter to display a List<> with a xml template on a ListView This List<> is filled with dataset which are given id's. 其中一个的ListFragments使用自定义ArrayAdapter显示List<>在一个XML模板ListViewList<>填充有被赋予ID的数据集。 Datasets with id's lower than 10 are flagged "important", the other ones are flagged "not so important" ID小于10的数据集被标记为“重要”,其他数据集被标记为“不太重要”

This is what I am looking for: I have two layout templates, and I want to display the "important" datasets with one layout and the "not so important" with the other layout. 这就是我要寻找的:我有两个布局模板,并且我想用一种布局显示“重要”数据集,而用另一种布局显示“不太重要”的数据集。

I have searched for quite a while now, but it looks like noone has asked a question like this before. 我已经搜索了很长时间,但似乎以前没有人问过这样的问题。 I tried to use a second ListView and apply a second custom ArrayAdapter on it, but unfortunately, there is a restriction: Since I'm using ListFragment , I'm forced to use findViewById like this. 我尝试使用第二个ListView并在其上应用第二个自定义ArrayAdapter ,但是很遗憾,存在一个限制:由于我使用的是ListFragment ,因此我不得不像这样使用findViewById

ListView list;
list = (ListView) rootView.findViewById(android.R.id.list);

When I tried to write the second custom ArrayAdapter , it showed an error here: 当我尝试编写第二个自定义ArrayAdapter ,它在此处显示错误:

ListView list;
list_flat = (ListView) rootView.findViewById(android.R.id.list_flat);

Here is the entire ListFragment 这是整个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);
        }
       });
}

} }

This is the xml file I pass on to my ArrayAdapter 这是我传递给 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>

This is the xml layout file for the ListFragment 这是 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>

You shouldn't give a ListView an ID like this : android:id="@+id/android:list2" , the ID of your ListView should be 您不应该给ListView的ID: android:id="@+id/android:list2" ,您的ListView的ID应该是

<ListView android:id="@id/android:list"   
...... ></ListView>

or 要么

<ListView android:id="@+id/sampleList"   
...... ></ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中使用arrayAdapter和Listfragment制作按钮列表? - How to make List of button using arrayAdapter and Listfragment in android? 具有两种布局的Android Listview ArrayAdapter - Android Listview ArrayAdapter with two layouts Android //从对话框刷新ListFragment中的自定义Arrayadapter - Android // Refresh a Custom Arrayadapter in a ListFragment from a Dialog 在Android上的ListFragment中使用ArrayAdapter获取构造函数错误 - Getting a constructor error with ArrayAdapter in a ListFragment on Android 如何在 android 中为不同的布局使用不同的 tabhosts - how to use different tabhosts for different layouts in android 可以在Android的ArrayAdapter getView()中膨胀两个布局吗? - Is it possible to inflate two layouts in ArrayAdapter getView() in Android? 针对不同的屏幕尺寸Android使用不同的布局 - Using different layouts for different screen sizes Android 在不同的Android屏幕上使用不同的XML布局? - Using different XML layouts for different Android screens? 如何在Android listiview中使用不同的行布局 - How to use different row layouts in Android listiview 如何指定要用于ListFragment的ArrayAdapter的字符串? - How do I specify which string of an ArrayAdapter to use for a ListFragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM