JSONArray无法转换为java.util.ArrayList

时间:2019-07-23 20:52:44

标签: java android json

我正在使用过滤器编写广告适配器类,并且在publishResults方法中收到此错误。 该列表已加载,当我在过滤器中键入内容时,它将开始过滤,但是当删除字符并达到0长度时,应用程序会因该错误而崩溃,此外,某些imageView(CardView类型)被颠倒了,所以也许我在做什么解析也是错误的。

片段

import glob
import os
from zipfile import ZipFile

def main():
    for fname in glob.glob("*.zip"):  # get all the zip files
        with ZipFile(fname) as archive:
            # if there's no file.txt, ignore and go on to the next zip file
            if 'file.txt' not in archive.namelist(): continue

            # make a new directory named after the zip file
            dirname = fname.rsplit('.',1)[0]
            os.mkdir(dirname)

            extract file.txt into the directory you just created
            archive.extract('file.txt', path=dirname)

适配器

public class ColorViewFragment extends Fragment {

    private RecyclerView recyclerView;
    private JSONArray json;
    private ColorListAdapter adapter;

    private EditText editColor;

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

        View view = inflater.inflate(R.layout.color_list, container, false);
        this.recyclerView = view.findViewById(R.id.recyclerView);

        /*
        try {
            this.recyclerView.setAdapter(new ColorListAdapter(this.json));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        */

        try {
            adapter = new ColorListAdapter(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        recyclerView.setAdapter(adapter);


        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        this.recyclerView.setLayoutManager(layoutManager);

        //
        editColor = view.findViewById(R.id.editText);
        editColor.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                ColorViewFragment.this.adapter.getFilter().filter(s);

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        return view;
    }

    public void setJSON(JSONArray newJson){
        this.json = newJson;
    }


}

1 个答案:

答案 0 :(得分:1)

当您返回JSONArray对象而不是第一个performFiltering(CharSequence constraint)中的ArrayList时,问题就会在您的if statement中发生

要解决此问题,请执行以下更改:

 public class colorFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults Result = new FilterResults();
            // if constraint is empty return the original names
            if(constraint.length() == 0 ) {
                ArrayList<String> arrColorList = new ArrayList<>();
                for (int i = 0; i < colorList.length(); i++) {                                                      
                    arrColorList.add(colorList.getJSONObject(i).getString("Name"));
                }
                Result.values = arrColorList;
                Result.count = arrColorList.size();
                return Result;
            }
            else {

                List<String> Filtered_Names = new ArrayList<String>();
                String filterString = constraint.toString().toLowerCase();
                String filterableString = "";

                for (int i = 0; i < colorList.length(); i++) {
                    try {
                        filterableString = (colorList.getJSONObject(i)).getString("Name");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    if (filterableString.toLowerCase().contains(filterString)) {
                        Filtered_Names.add(filterableString);
                    }
                }

                Result.values = Filtered_Names;
                Result.count = Filtered_Names.size();
                return Result;

            }

        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            colorListFiltered = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }
    }