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
(getActivity(),
android.R.layout.simple_list_item_1, model.getIncidents()));
it says The constructor ArrayAdapter
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(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(getActivity(),
android.R.layout.simple_list_item_1, model.getIncidents()));
return view;
}
public class MyArrayAdapter extends ArrayAdapter- {
ArrayListitems;
Context context;
public MyArrayAdapter(Context context, int textViewResourceId,
ArrayListitems) {
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
.
stackoverflow.comm
No comments:
Post a Comment