通过数组适配器设置适配器:Android

时间:2015-08-14 11:46:15

标签: android listview arraylist

我想创建一个自定义listView并向其添加一些数据,但是当taskList.setAdapter(适配器)时它有一个异常;跑了

public class FragmentTasks extends Fragment {
    ListView taskList;
    ArrayList<TaskItems> mTaskItems = new ArrayList<TaskItems>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.taks_layout, container, false);
        mTaskItems.add(new TaskItems("mgh", R.drawable.ic_done));
        mTaskItems.add(new TaskItems("za", R.drawable.ic_not_done));

        taskList = (ListView) view.findViewById(R.id.userMenu);
        TaskListAdapter adapter = new TaskListAdapter(getActivity(), mTaskItems);
        taskList.setAdapter(adapter);
        return view;
    }

    class TaskItems {
        String mTaskTitle;
        int mTaskStatusIcon;

        public TaskItems(String taskTitle, int taskStatusIcon) {
            mTaskTitle = taskTitle;
            mTaskStatusIcon = taskStatusIcon;
        }
    }

    class TaskListAdapter extends BaseAdapter {
        Context mContext;
        ArrayList<TaskItems> mTaskItems;

        public TaskListAdapter(Context context, ArrayList<TaskItems> taskItems) {
            mContext = context;
            mTaskItems = taskItems;
        }

        @Override
        public int getCount() {
            return mTaskItems.size();
        }

        @Override
        public Object getItem(int position) {
            return mTaskItems.get(position);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            View view;
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.tasks_list, null);
            } else {
                view = convertView;
            }
            TextView taskTitle = (TextView) view.findViewById(R.id.tvTaskTitle);
            ImageButton btnTaskStatus = (ImageButton) view.findViewById(R.id.btnTaskStatus);

            taskTitle.setText(mTaskItems.get(position).mTaskTitle);
            btnTaskStatus.setBackgroundResource(mTaskItems.get(position).mTaskStatusIcon);

            return view;
        }
    }
}

,错误代码为

08-14 15:51:10.315  19661-19661/com.saniyeh.iwedding E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.saniyeh.iwedding.FragmentTasks.onCreateView(FragmentTasks.java:33)
            at android.app.Fragment.performCreateView(Fragment.java:1695)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
            at android.app.BackStackRecord.run(BackStackRecord.java:694)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
问题在哪里?什么是创建列表的正确方法

4 个答案:

答案 0 :(得分:4)

更改

@Override
        public long getItemId(int i) {
            return 0;
        }

@Override
        public long getItemId(int i) {
            return i;
        }

答案 1 :(得分:3)

替换

view = inflater.inflate(R.layout.tasks_list, null);

View view =  inflater.inflate(R.layout.tasks_list,viewGroup,false);

答案 2 :(得分:1)

使用ArrayList<TaskItems> mTaskItems;

更改ArrayList<TaskItems> mTaskItems=new ArrayList<>();

convertView = inflater.inflate(R.layout.tasks_list,viewGroup false);

答案 3 :(得分:1)

尝试这个...

  public class ViewHolder{
   TextView taskTitle;
  ImageButton btnTaskStatus;
  }

    @Override
    public long getItemId(int i) {
        return i;
    }
  @Override
    public View getView(int position, View convertView, ViewGroup viewGroup)                                                    
    {
      ViewHolder viewHolder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.tasks_list, null);
            viewHolder=new ViewHolder();
            viewHolder.taskTitle=(TextView) view.findViewById(R.id.tvTaskTitle);
            viewHolder.btnTaskStatus=(ImageButton) view.findViewById(R.id.btnTaskStatus);
           convertView.setTag(viewHolder);
        } else {
            viewHolder=(ViewHolder)convertView.getTag();
        }
        taskTitle.setText(mTaskItems.get(position).mTaskTitle);
        btnTaskStatus.setBackgroundResource(mTaskItems.get(position).mTaskStatusIcon);

        return convertView;
    }