metemet06
metemet06

Reputation: 1472

ListFragment and ListView

I cant extend my class to ListFragment.It gives me ClassCastException error. The code was working while extending ListActivity.And it also works while extending Fagment (when lists not used). But ListFragment gives errors, and I couldnt find solution. Is there any way to solve it? Maybe an inner class that extends ListFragment/ListActivity may solve problem but I couldnt achieve it .

I want to set adapters:

setListAdapter(Adapter<String>(this, R.layout.row));
m_List = (ArrayAdapter<String>)getListAdapter();

xml

<ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
    <TextView
        android:id="@+id/android:empty"
        android:text=""
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"/>

activity

public class MyActivity extends Fragment  {

    public class MyListFragment extends ListActivity {        
               @Override
            public void onCreate(Bundle savedInstanceState) {
                   super.onCreate(savedInstanceState);    
                setContentView(R.layout.stopwatch);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.row));               
             m_List = (ArrayAdapter<String>)getListAdapter();

          }

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            v = inflater.inflate(R.layout.stopwatch, container, false);
    MyListFragment ma = new MyListFragment();  // ???
    }

    }

Upvotes: 0

Views: 1978

Answers (2)

user3133317
user3133317

Reputation: 83

I would write a new class called ListFragment, and migrate the code over:

Here's a step by step guide for a basic scenario:

1)Create a new fragment.java file, with a super plain implementation of a fragment.

package com.a;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class MenuFragment extends ListFragment 
{

public static final String TAG = "MenuFragment";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.list, null);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    //
    // insert code here to setup and call setListAdapter()
    //
}

@Override
public void onListItemClick(ListView lv, View v, int position, long id) 
{

}
}

2)Look for your setContentView in your listactivity code, use that layout as a parameter to the inflate() call under onCreateView()

setContentView(R.layout.main);

to

View v = inflater.inflate(R.layout.main, null);

3)Move the rest of your code under your listactivity onCreate() to listfragmentt OnCreateView(). You probably have some code setting up the controls such as the below:

text = (EditText) this.findViewById(R.id.text);

It will become this:

text = (EditText) v.findViewById(R.id.text);

Note that v is the view, returned from the inflate call.

4)If your code has references to "this", you will get an error. Replace that with "getActivity()".

5)Check what code you have left, and find the right place to put them in the new file. Hopefully the above covers the basics.

Upvotes: 0

Vallabh Lakade
Vallabh Lakade

Reputation: 832

if you use ListActivity then:

 public class MyListActivity extends ListActivity {
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
  }
} 

if Fragment not Fragment but ListFragment then

public class CountryList extends ListFragment {

    String[] countries = new String[] {
        "India",
        "Pakistan",
        "Sri Lanka",
        "China",
        "Bangladesh",
        "Nepal",
        "Afghanistan",
        "North Korea",
        "South Korea",
        "Japan"
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        /** Creating an array adapter to store the list of countries **/
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1,countries);

        /** Setting the list adapter for the ListFragment */
        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }
}

Upvotes: 4

Related Questions