IrishWhiskey
IrishWhiskey

Reputation: 339

Android ListFragment Errors

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

Upvotes: 2

Views: 698

Answers (1)

Sharjeel
Sharjeel

Reputation: 15798

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.

Upvotes: 2

Related Questions