Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从listview中删除多个选定行项目?_Android_Listview_Android Listview_Null_Nullpointerexception - Fatal编程技术网

Android 如何从listview中删除多个选定行项目?

Android 如何从listview中删除多个选定行项目?,android,listview,android-listview,null,nullpointerexception,Android,Listview,Android Listview,Null,Nullpointerexception,我实现了一种简单的方法,按照此解决方案从列表视图中删除多个项目,然后对其进行了一点修改,以允许对两个按钮单击事件使用切换语句,add&delete。但问题是,当我单击delete按钮时,应用程序崩溃,出现以下错误: 我不确定为什么会出现空指针异常,因为我相信我已经正确分配了所有内容 有人能更好地解释我为什么会犯这个错误吗?或者是从列表视图中删除选定行项目的更好方法 完整的课程发布在下面,以便更好地理解: public class TopRatedFragment extends Fragment

我实现了一种简单的方法,按照此解决方案从列表视图中删除多个项目,然后对其进行了一点修改,以允许对两个按钮单击事件使用切换语句,
add
&
delete
。但问题是,当我单击delete按钮时,应用程序崩溃,出现以下错误:

我不确定为什么会出现空指针异常,因为我相信我已经正确分配了所有内容

有人能更好地解释我为什么会犯这个错误吗?或者是从列表视图中删除选定行项目的更好方法

完整的课程发布在下面,以便更好地理解:

public class TopRatedFragment extends Fragment implements OnClickListener {

    ListView mListView;
    EditText mValue;
    Button mAdd,mDel;
    ArrayList<String> list = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    SparseBooleanArray mCheckStates ;


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

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
         mAdd = (Button)rootView.findViewById(R.id.newList);
         mDel = (Button)rootView.findViewById(R.id.delBtn);
         mAdd.setOnClickListener(this);
         mDel.setOnClickListener(this);
         mValue = (EditText)rootView.findViewById(R.id.listData);
         adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1, list);

    // set the lv variable to your list in the xml
         mListView=(ListView)rootView.findViewById(R.id.listView);  
         mListView.setAdapter(adapter);


        return rootView;    
    }


    public void onClick(View v)
    {
        switch(v.getId()){
        case R.id.newList:
             //DO something
            String input = mValue.getText().toString();
            if(input.length() > 0)
            {
                // add string to the adapter, not the listview
                adapter.add(input);
                // no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
            }
        break;
        case R.id.delBtn:
             //DO something
            SparseBooleanArray checked = mListView.getCheckedItemPositions();
            for (int i = 0; i < mListView.getCount(); i++){

                //line 65
                if (checked.get(i)==true)
                {
                     list.remove(i);

                } 
                adapter.notifyDataSetChanged(); 

            }
             mListView.clearChoices();               
        }


    }   

}
公共类TopRatedFragment扩展片段实现OnClickListener{
列表视图;
编辑文本价值;
按钮制造,mDel;
ArrayList=新建ArrayList();
阵列适配器;
Sparseboolean数组mCheckStates;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图根视图=充气机。充气(R.layout.fragment\u top\u rated,容器,false);
mAdd=(按钮)rootView.findviewbyd(R.id.newList);
mDel=(按钮)rootView.findviewbyd(R.id.delBtn);
mAdd.setOnClickListener(此);
mDel.setOnClickListener(此);
mValue=(EditText)rootView.findViewById(R.id.listData);
adapter=new ArrayAdapter(getActivity(),android.R.layout.simple\u expandable\u list\u item\u 1,list);
//将lv变量设置为xml中的列表
mListView=(ListView)rootView.findviewbyd(R.id.ListView);
mListView.setAdapter(适配器);
返回rootView;
}
公共void onClick(视图v)
{
开关(v.getId()){
案例R.id.newList:
//做点什么
字符串输入=mValue.getText().toString();
if(input.length()>0)
{
//将字符串添加到适配器,而不是listview
adapter.add(输入);
//无需调用adapter.notifyDataSetChanged();因为它是由adapter.add()方法完成的
}
打破
案例R.id.delBtn:
//做点什么
SparseBooleanArray checked=mListView.GetCheckEditePositions();
对于(int i=0;i

从适配器中删除,而不是从listview中删除尝试以下操作:

 SparseBooleanArray checked = mListView.getCheckedItemPositions();

  if(checked!=null){
  for (int i = 0; i < mListView.getCount(); i++){

            //line 65
            if (checked.get(i)==true)
            {
                 adapter.remove(mListView.getItemAtPosition(i));

            } 
            adapter.notifyDataSetChanged(); 

        }
         mListView.clearChoices(); 
   }
SparseBooleanArray checked=mListView.getCheckedItemPositions();
如果(选中!=null){
对于(int i=0;i
GetCheckEditePositions返回: SparseBooleanArray,对于每次调用get(int position)都返回true,其中position是列表中的选中位置,否则返回false;如果选择模式设置为choice\u mode\u NONE,则返回null

您需要使用valueAt(),工作代码应该是

SparseBooleanArray checkedItems = mListView.getCheckedItemPositions();
if (checkedItems != null) {
    for (int i=0; i<checkedItems.size(); i++) {
        if (checkedItems.valueAt(i)) {
            String item = mListView.getAdapter().getItem(
                                  checkedItems.keyAt(i)).toString();
            Log.i(TAG,item + " was selected");
        }
    }
}
SparseBooleanArray checkedItems=mListView.getCheckedItemPositions();
if(checkedItems!=null){

对于(int i=0;i我用这种方式使用循环,它工作:

SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
                int itemCount = getListView().getCount();

                for(int i=itemCount-1; i >= 0; i--){
                    if(checkedItemPositions.get(i)){
                        adapter.remove(list.get(i));
                    }
                }
                checkedItemPositions.clear();
                adapter.notifyDataSetChanged();

这个`if(checked.get(i)==true)`如何填充列表。我猜它的空i通过在edittext中输入项并单击add按钮填充列表。首先要看到的是您的“checked”变量为空。找出它为什么为空,然后您就会有一些事情发生。如果您不能找出为什么为“getCheckItemPositions”返回null,在这里读取,另外,绝对不要将ListView中的choice mode设置为choice_mode_NONE。@BrianJ是的。这应该可以工作为什么
if(checked.get(i)==true)
第65行抛出NPE??在for循环之前检查null反向for循环:从末尾开始。