2

这是我第一次使用 ListFragments,我遇到了错误。所以我的问题是为什么我在这行代码上得到一条红线?

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

它说The constructor ArrayAdapter<String>(Activity, int, ArrayList<Incident>) is undefined

编辑

我不再收到此片段的上述错误。我稍微修改了代码,现在看起来像这样:

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;
}
}

但是现在我得到一个运行时错误。logcat在下面

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)

我终其一生都无法弄清楚他们在第一行所说的内容。我没有看到任何R.id名为list. 我现在相信我只需要在某个地方放置一个列表视图或文本视图。谢谢你尽你所能的帮助

编辑

这是我的片段的新代码

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();
    }

}
}

我下面有红线 Item item = items.get(position);

holder.userName = (TextView) row.findViewById(R.id.name);
“无法解析用户名”

holder.name.setText(item.getName());
“方法 getName() 未定义 ClipData.Item 类型”

public static class ViewHolder
“成员类型 ViewHolder 不能声明为静态;静态类型只能声明为静态或顶级类型”

因此,如果我们可以清除这些红线,它可能会起作用,也可能不会起作用。如果您希望我继续走这条路,请告知此代码是否在正确的位置,以及需要进行哪些编辑

4

1 回答 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.

于 2013-04-21T01:49:03.747 回答