J
J
javaProger982019-06-19 17:18:34
Java
javaProger98, 2019-06-19 17:18:34

How to output data from xml to listView?

It is necessary to display data from xml by link in listview.
Everything is correctly unloaded into the ArrayList list,
the problem is in the ListAdapter.
I attach the code

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.*;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    ProgressDialog progressDialog;
    ArrayList<Valute> list = new ArrayList<>();
   private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView)findViewById(R.id.lv);


        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Загрузка...");
        progressDialog.setCancelable(false);

        new Async().execute();

    }
    private List<String> name(){
        List<String>list =new ArrayList<>();
        return list;
    }
    private class Async extends AsyncTask<String, String, String>
    {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.show();
        }

        @Override
        protected String doInBackground(String... strings){
            try {
               Document document = Jsoup.connect("https://www.cbr-xml-daily.ru/daily_utf8.xml")
                       .timeout(3000).followRedirects(true).ignoreContentType(true).userAgent("Mozilla")
                       .get();

                Elements elements = document.select("Valute");

              for(Element element: elements)

                {

                 Valute valute = new Valute();
                 valute.setName(element.select("Name").text());

                 valute.setValue(element.select("Value").text());
                 list.add(valute);


                }




            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }


        @Override
        protected void onPostExecute(String s) {
           progressDialog.dismiss();



        ListAdapter listAdapter = new ListAdapter(MainActivity.this,list);
            lv.setAdapter(listAdapter);
      
        }
    }
}



ListAdapter.java
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

public class ListAdapter extends BaseAdapter {
    Context context;
    ArrayList<Valute> list;
    LayoutInflater layoutInflater;
    ListAdapter(Context context,ArrayList<Valute> list){
        this.context=context;
        this.list =list;
       // layoutInflater = LayoutInflater.from(context);
        layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int i) {
        return false;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver dataSetObserver) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {

    }

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

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        return null;
    }

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

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

    @Override
    public boolean isEmpty() {
        return false;
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
davidnum95, 2019-06-19
@davidnum95

First:
https://developer.android.com/reference/android/wi...
Secondly, you need to correctly implement the methods

public int getCount()
 public View getView(int i, View view, ViewGroup viewGroup)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question