将动态值添加到Fragment中的listview

时间:2014-05-20 19:32:24

标签: android listview fragment

我想在片段中点击按钮时将微调器值添加到listview。实际上我的屏幕包含两个微调器和一个列表视图。我只想在按钮单击时将微调器值添加到列表中。我已经为它写了一些代码,但我得到了空指针异常。这是我的代码:

   public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_thirdswipe, container, false);
    arraylistAdd = new ArrayList<DeviceInfo>();
    spinner = (Spinner) rootView.findViewById(R.id.spnDevice);

    spinnerArrayAdapter = new ArrayAdapter<String>(rootView.getContext(), R.layout.spinner_items, spinnerArray); //selected item will look like a spinner set from XML
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerArrayAdapter);
    spinner = (Spinner) rootView.findViewById(R.id.spnAction);
    spinnerArrayAdapter = new ArrayAdapter<String>(rootView.getContext(), R.layout.spinner_items, spinnerArray); //selected item will look like a spinner set from XML
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerArrayAdapter);
    btnAdd = (Button) rootView.findViewById(R.id.buttonAdd);

    listOnAdd = (ListView) rootView.findViewById(R.id.listDynamic);     
    deviceType = spinner.getSelectedItem().toString();
    actionType = spinner.getSelectedItem().toString();
    Log.i("item", deviceType +" "+ actionType);
    adp=new CustomListDynamic(getActivity(),R.layout.custom_add_list,arraylistAdd);
    btnAdd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            info = new DeviceInfo();
            info.setAction(actionType);
            info.setDevice(deviceType);

            arraylistAdd.add(info);
            for(int i=0;i<arraylistAdd.size();i++){
                Log.i("itemArray", arraylistAdd.get(i).getDevice());
            }
            adp.notifyDataSetChanged();
        }
    });
    listOnAdd.setAdapter(adp);

    return rootView;
}

我的customAdpter类如下:

    public class CustomListDynamic  extends ArrayAdapter<DeviceInfo> {
Context context;
List<DeviceInfo> objects;   
int layoutResourceId;

    public CustomListDynamic(Context context, int layoutResourceId,
            List<DeviceInfo> rowItems) {
        super(context, layoutResourceId, rowItems);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        objects = rowItems;

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

    @Override
    public long getItemId(int position) {
        return position;
    }
       private class ViewHolder {

            TextView txtDeviceType;
            TextView txtActionType;
        }
       public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            final DeviceInfo rowItem = getItem(position);

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {

                convertView = inflater.inflate(R.layout.custom_add_list, null);
                holder = new ViewHolder(); 
                holder.txtDeviceType = (TextView) convertView.findViewById(R.id.txtDeviceType);
                holder.txtDeviceType = (TextView) convertView.findViewById(R.id.txtMobileNumber);     
                convertView.setTag(holder);
            } else
                holder = (ViewHolder) convertView.getTag();
                holder.txtDeviceType.setText(rowItem.getDevice());
                holder.txtActionType.setText(rowItem.getAction());
                Log.i("device set", rowItem.getDevice());
            return convertView;
        }          
}

logcat的:

 05-20 18:41:49.301: E/AndroidRuntime(2083):    at       android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
 05-20 19:08:34.661: E/AndroidRuntime(2439): FATAL EXCEPTION: main
 05-20 19:08:34.661: E/AndroidRuntime(2439): java.lang.NullPointerException
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at com.example.test.CustomListDynamic.getView(CustomListDynamic.java:66)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.AbsListView.obtainView(AbsListView.java:2159)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.ListView.makeAndAddView(ListView.java:1831)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.ListView.fillDown(ListView.java:674)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.ListView.fillFromTop(ListView.java:735)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.ListView.layoutChildren(ListView.java:1638)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.AbsListView.onLayout(AbsListView.java:1994)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.view.View.layout(View.java:14008)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.view.View.layout(View.java:14008)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.view.View.layout(View.java:14008)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.view.View.layout(View.java:14008)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1141)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.view.View.layout(View.java:14008)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
 05-20 19:08:34.661: E/AndroidRuntime(2439):    at android.view.View.layout(View.java:14008)

如果有人感谢,请告诉我解决方案

2 个答案:

答案 0 :(得分:1)

尝试使用此更改代码,

public class CustomListDynamic  extends ArrayAdapter<DeviceInfo> {
Context context;
List<DeviceInfo> objects;   
int layoutResourceId;

    public CustomListDynamic(Context context, int layoutResourceId,
            List<DeviceInfo> rowItems) {
        super(context, layoutResourceId, rowItems);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        objects = rowItems;

    }

       private class ViewHolder {

            TextView txtDeviceType;
            TextView txtActionType;
        }

       public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            DeviceInfo rowItem = object.get(position);

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {

                convertView = inflater.inflate(R.layout.custom_add_list, null);
                holder = new ViewHolder(); 
                holder.txtDeviceType = (TextView) convertView.findViewById(R.id.txtDeviceType);
                holder.txtDeviceType = (TextView) convertView.findViewById(R.id.txtMobileNumber);     
                convertView.setTag(holder);

            } else
                holder = (ViewHolder) convertView.getTag();

              holder.txtDeviceType.setText(rowItem.getDevice());
              holder.txtActionType.setText(rowItem.getAction());
              Log.i("device set", rowItem.getDevice());
            return convertView;
        }          
     }

的变化:

删除getItemIdgetCount因为您不需要ArrayAdapter,您需要BaseAdapter类中的那些,super构造函数类句柄该

更改

 final DeviceInfo rowItem = getItem(position);

 DeviceInfo rowItem = objects.get(position);

<强>更新

如果您有错误

 "holder.txtActionType.setText(rowItem.getAction());"

存在三种可能性。

1- holder.txtActionType为空

2- rowItem为空

3- rowItem.getAction()为空

您的问题:

您未在代码中初始化holder.txtActionType

我认为这是复制粘贴问题:

holder.txtDeviceType = (TextView) convertView.findViewById(R.id.txtDeviceType);
holder.txtDeviceType = (TextView) convertView.findViewById(R.id.txtMobileNumber); 

您初始化txtDeviceType这两行必须是holder.txtActionType

答案 1 :(得分:0)

当您在此时单击按钮时,您必须为适配器设置数据。然后你必须编写notifidatasetchange方法。

在您的情况下,您将获得rowItem值为null,表示List<DeviceInfo> objects;也获得null对象值。

现在你的代码是

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_thirdswipe, container, false);
    arraylistAdd = new ArrayList<DeviceInfo>();
    spinner = (Spinner) rootView.findViewById(R.id.spnDevice);

    spinnerArrayAdapter = new ArrayAdapter<String>(rootView.getContext(), R.layout.spinner_items, spinnerArray); //selected item will look like a spinner set from XML
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerArrayAdapter);
    spinner = (Spinner) rootView.findViewById(R.id.spnAction);
    spinnerArrayAdapter = new ArrayAdapter<String>(rootView.getContext(), R.layout.spinner_items, spinnerArray); //selected item will look like a spinner set from XML
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerArrayAdapter);
    btnAdd = (Button) rootView.findViewById(R.id.buttonAdd);

    listOnAdd = (ListView) rootView.findViewById(R.id.listDynamic);     
    deviceType = spinner.getSelectedItem().toString();
    actionType = spinner.getSelectedItem().toString();
    Log.i("item", deviceType +" "+ actionType);
    adp=new CustomListDynamic(getActivity(),R.layout.custom_add_list,arraylistAdd);
    btnAdd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            info = new DeviceInfo();
            info.setAction(actionType);
            info.setDevice(deviceType);

            arraylistAdd.add(info);
            for(int i=0;i<arraylistAdd.size();i++){
                Log.i("itemArray", arraylistAdd.get(i).getDevice());
            }
            **adp.setObjectValue(info);** //set value of your object from here
            adp.notifyDataSetChanged();
        }
    });
    listOnAdd.setAdapter(adp);

    return rootView;
}

您的Custome Adapter类就像This

 public class CustomListDynamic  extends ArrayAdapter<DeviceInfo> {
Context context;
List<DeviceInfo> objects;   
int layoutResourceId;

    public CustomListDynamic(Context context, int layoutResourceId,
            List<DeviceInfo> rowItems) {
        super(context, layoutResourceId, rowItems);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        objects = rowItems;

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

    public void setObjectValue(List<DeviceInfo> info){
       objects=info;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
       private class ViewHolder {

            TextView txtDeviceType;
            TextView txtActionType;
        }
       public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            final DeviceInfo rowItem = getItem(position);

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {

                convertView = inflater.inflate(R.layout.custom_add_list, null);
                holder = new ViewHolder(); 
                holder.txtDeviceType = (TextView) convertView.findViewById(R.id.txtDeviceType);
                holder.txtDeviceType = (TextView) convertView.findViewById(R.id.txtMobileNumber);     
                convertView.setTag(holder);
            } else
                holder = (ViewHolder) convertView.getTag();
                holder.txtDeviceType.setText(rowItem.getDevice());
                holder.txtActionType.setText(rowItem.getAction());
                Log.i("device set", rowItem.getDevice());
            return convertView;
        }          
}