2

This is my first time using ListFragments and I'm running into an error. So my question is why am I getting a red line on this line of code?

setListAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, model.getIncidents()));

it says The constructor ArrayAdapter<String>(Activity, int, ArrayList<Incident>) is undefined

Edit

I no longer get the above error for this fragment. I altered the code slightly and now it looks like this:

public class Incidents_Frag extends ListFragment {
View view;
public static Model model;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {        
    view = inflater.inflate(R.layout.incid, container, false);
    model = new Model();
    setListAdapter(new ArrayAdapter<Incident>(getActivity(),
            android.R.layout.simple_list_item_1, model.getIncidents()));        
    return view;
}
}

However now I get a runtime error. The logcat is below

04-21 18:47:40.934: E/AndroidRuntime(806): FATAL EXCEPTION: main
04-21 18:47:40.934: E/AndroidRuntime(806): java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
04-21 18:47:40.934: E/AndroidRuntime(806):  at android.app.ListFragment.ensureList(ListFragment.java:402)
04-21 18:47:40.934: E/AndroidRuntime(806):  at android.app.ListFragment.onViewCreated(ListFragment.java:203)
04-21 18:47:40.934: E/AndroidRuntime(806):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:809)
04-21 18:47:40.934: E/AndroidRuntime(806):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
04-21 18:47:40.934: E/AndroidRuntime(806):  at android.app.BackStackRecord.run(BackStackRecord.java:622)
04-21 18:47:40.934: E/AndroidRuntime(806):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
04-21 18:47:40.934: E/AndroidRuntime(806):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:417)
04-21 18:47:40.934: E/AndroidRuntime(806):  at android.os.Handler.handleCallback(Handler.java:605)
04-21 18:47:40.934: E/AndroidRuntime(806):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-21 18:47:40.934: E/AndroidRuntime(806):  at android.os.Looper.loop(Looper.java:137)
04-21 18:47:40.934: E/AndroidRuntime(806):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-21 18:47:40.934: E/AndroidRuntime(806):  at java.lang.reflect.Method.invokeNative(Native Method)
04-21 18:47:40.934: E/AndroidRuntime(806):  at java.lang.reflect.Method.invoke(Method.java:511)
04-21 18:47:40.934: E/AndroidRuntime(806):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-21 18:47:40.934: E/AndroidRuntime(806):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-21 18:47:40.934: E/AndroidRuntime(806):  at dalvik.system.NativeStart.main(Native Method)

I cannot for the life of me figure out what they're talking about in the first line. I don't see any R.id files with the name list. I now believe I simply need to put a listview or a textview somewhere though. thanks for any help you can offer

Edit

Here is the new code for my fragment

public class Incidents_Frag extends ListFragment {
View view;
public static Model model;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {        
    view = inflater.inflate(R.layout.incid, container, false);
    model = new Model();
    setListAdapter(new ArrayAdapter<Incident>(getActivity(),
            android.R.layout.simple_list_item_1, model.getIncidents()));        
    return view;
}

public class MyArrayAdapter extends ArrayAdapter<Item> {
    ArrayList<Incident> items;
    Context context;
    public MyArrayAdapter(Context context, int textViewResourceId,
            ArrayList<Incident> items) {
        super(context, textViewResourceId, items);

        this.context = context;
        this.items = items; 
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        ViewHolder holder = new ViewHolder();
        Item item = items.get(position);

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(R.layout.list_item, parent, false);
            holder.userName = (TextView) row.findViewById(R.id.name);
            row.setTag(holder);
        }
        else
        { 
            holder = (ViewHolder) row.getTag();
        }

        holder.name.setText(item.getName());
        return row;
    }

    public static class ViewHolder
    {
        TextView name;
    }

    public int getCount() {
        return items.size();
    }

}
}

I get red lines under Item item = items.get(position);

holder.userName = (TextView) row.findViewById(R.id.name);
"userName cannot be resolved"

holder.name.setText(item.getName());
"The method getName() is undefined for the type ClipData.Item"

public static class ViewHolder
"The member type ViewHolder cannot be declared static; static types can only be declared in static or top level types"

So if we can clear up these red lines, it may or may not work. If you would like me to continue down this path please advise on whether this code is in the right place, and what edits need to be done

4
  • 1
    ArrayAdapter is a typed class, so in order for the constructor to accept a list of Incident objects, you'll need to call: new ArrayAdapter<Incident>(...). Commented Apr 21, 2013 at 5:01
  • 1
    Regarding your new problem: If you've made any changes to layout files, search for any id's within the @android namespace. In particular, keep an eye out for id's that look like @android:id/list. The error suggests you've given some view an id that a ListFragment uses to identify the ListView in the layout file with. I'd start my search in R.layout.incid (incid.xml). Commented Apr 21, 2013 at 19:29
  • @MH Ya, I think the error appears to be obvious, what I can't wrap my head around is this list fragment's xml file is empty. It simply has the linear layout defined. There are no listviews in any other activity/fragment either and they work/display what they are supposed to. Commented Apr 21, 2013 at 20:15
  • If your xml file is empty, then don't use it as view for the fragment. You should either inflate a layout that contains a ListView with an id of @android:id/list, or let the ListFragment use its predefined layout by not inflating a custom layout. Refer to this Screen Layout section in the docs for more details . Commented Apr 21, 2013 at 20:35

1 Answer 1

2

Default constructor only accepts an Array of String. You can passing an Array of "Incident" hence that error.

You will have to implement your own ArrayAdapter that accepts array of object type "Incident".

A sample extended ArrayAdapter will look something like this.

public class MyArrayAdapter extends ArrayAdapter<Incident> { // changed from Item to Incident
    ArrayList<Incident> items;
    Context context;
    public MyArrayAdapter(Context context, int textViewResourceId,
            ArrayList<Incident> items) {
        super(context, textViewResourceId, items);

        this.context = context;
        this.items = items; 
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        ViewHolder holder = new ViewHolder();
        Incident item = items.get(position); // item is your accident

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(R.layout.list_item, parent, false);
            holder.name = (TextView) row.findViewById(R.id.name); // updated it is name from holder class
            row.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) row.getTag();
        }

        holder.name.setText(item.getName()); // this is just example item is your Incident, you have to get it's name function whatever that is in your class
        return row;
    }

    public static class ViewHolder
    {
        TextView name;
    }

    public int getCount() {
        return items.size();
    }

}

You will have to create your own layout for this. For above example create list_item.xml and just add one TextView with id name and it will work.

Sign up to request clarification or add additional context in comments.

5 Comments

When I attempted to make the custom adapter I ended up with more errors and some code I didn't fully understand. I appreciate the effort though. Check out the edited post, I think I'm on the right track, just need a little guidance
Default adapter will not understand your Incident object. It only accepts String. You will have to create your own adapter. Use the above code and update your question with new code and errors. It will work.
updated with new fragment code, please let me know what the next step is because I have quite a few red lines in my code now
im just gonna scrap this and turn it into a new question. This has now become a totally different question
I updated my answer. It was code from my project where my Object was Item but in your program it is Incident. So you have to update this code accordingly. Also you can Google a Google example of custom ArrayAdapter, it will help you because that's exactly what you need for handling your own objects in ArrayList.

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.