无法将Xml数据解析到RecyclerView

时间:2019-05-14 15:50:36

标签: java android xml android-recyclerview

我有一个RecyclerView,它应该从XML文件中获取数据。我创建了一种方法,该方法从包含XML内容的变量获取输入文件,然后循环该数据并将XML(子节点)的XML添加到列表(例如:ArrayList nameofList)。该列表用于onBind方法中。

我的问题是运行回收站的应用程序什么也没显示! 下面的一些代码:

编辑:进行建议的更改


适配器(编辑)

public class Adapter_recycler extends   
RecyclerView.Adapter<RecyclerView.ViewHolder> 
{


private String lista;
private ArrayList<Node> listaNodi;
private ArrayList<String> lista_errore;

public Adapter_recycler(String lista,RecyclerView recyclerView) {
    this.lista = lista;
    listaNodi = new ArrayList<>();


    lista_errore = new ArrayList<>();
    for (int i=0;i<=5;i++)
    lista_errore.add("ERRORE");

    try {
        LeggiFile();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }

}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v;
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.righe_main,parent,false);
            return new ViewHolder1(v);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {


    if(listaNodi.size() == 0)
    {
        ((ViewHolder1)viewHolder).titolo1.setText(lista_errore.get(position));
    }else{
        ((ViewHolder1)viewHolder).titolo1.setText(((Element) listaNodi.get(position)).getAttribute("name"));
    }

}

@Override
public int getItemCount() {
   if (listaNodi.size() == 0)
    return lista_errore.size();
   else
       return  listaNodi.size();
}

XML方法(循环并添加到列表中)(编辑)

private void LeggiFile() throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder DBuilder = factory.newDocumentBuilder();

    Node Players = DBuilder.parse(new InputSource(new StringReader(lista)));
    Node player = null;

    for (int i = 0; i < Players.getChildNodes().getLength(); i++) {
        if (player == null) {
            player = Players.getFirstChild();
        }
        else
            {player  = player.getNextSibling() ;}
        listaNodi.add(player);
    }

}

片段(编辑)

public class Fragment_Chat extends Fragment {

private Adapter_recycler adapter;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private String list = "";
Context context = getActivity();


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_fragment__chat, container,  
false);


    list = ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<Players>\n" +
            "<player name=\"mike\">\n" +
            "</player>\n" +
            "<player name=\"mike\">\n" +
            "</player>\n" +
            "<player name=\"mike\">\n" +
            "</player>\n" +
            "</Players>");


    recyclerView = view.findViewById(R.id.recycler);
    linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(linearLayoutManager);
    adapter = new Adapter_recycler(list,recyclerView,context);

    recyclerView.setAdapter(adapter);


    return view;

}

}

1 个答案:

答案 0 :(得分:1)

我马上就能看到变化

docker load

@Override
public int getItemCount() {
   return 0;
}

@Override public int getItemCount() { return listaNodi.size(); } 构造函数未实例化字段Adapter_recycler

如果仍不能解决问题,则可以在listaNodi中放置一个断点,并确保onBindViewHolder有项目。

移动

listaNodi

try { LeggiFile(); .. .. 构造函数,否则它将在每次需要视图时运行

DBuilder.parse可以使用Adapter_recycler,不需要String