1

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. 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. Datasets with id's lower than 10 are flagged "important", the other ones are flagged "not so important"

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 list;
list = (ListView) rootView.findViewById(android.R.id.list);

When I tried to write the second custom ArrayAdapter, it showed an error here:

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

Here is the entire 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

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

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

1 Answer 1

1

You shouldn't give a ListView an ID like this :android:id="@+id/android:list2", the ID of your ListView should be

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

or

<ListView android:id="@+id/sampleList"   
...... ></ListView>
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for replying! Very true! But then again, I already have a ListView with the line android:id="@id/android:list" in this xml file
android:id="@id/android:list" is same as the first pattern in the answer, if you have another list,use the second pattern, instead of android:id="@+id/android:list2", use android:id="@+id/list2"
Unfortunately, I can't simply use another list in a ListFragment. Maybe I can add a list into the ListFragment? I can't add a View to my list, it says "This method is not supported and throws an UnsupportedOperationException". It seems like I need to move away from my ListFragment to a standard Fragment
Maybe two different ListFragments, because if you want a List there, you have to use ListFragment instead of Fragment, if you only have one ListView for each fragment, you don't need to worry that all the ListViews have the same IDs, you can give all the ListView IDs in the first pattern
So you are telling me that I can use two ListFragments which I can display on one page?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.