How to insert SOAP web service data in List view VS 2017 Xamarin Android

Hi All,

I have consumed web service in project >> References >> Web references.

NavSOAPService1.Companies compTablebuffer = new NavSOAPService1.Companies();

        Companies_Service companyList = new Companies_Service();
        {
            NetworkCredential credential = new NetworkCredential("administrator", "itreenav");
            //companyList.UseDefaultCredentials = true;
        }

        NavSOAPService1.Companies[] list = companyList.ReadMultiple(null, null, 0);

        //compList.Add(compTablebuffer.Name);
        foreach (NavSOAPService1.Companies comp in list)
        {
            compList.Add(comp.Name);
        }

        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, compList);
        listView.Adapter = adapter;

}

This code I have written in .CS for list view but its not working If I made list as hard coding its opening in listview. And I have called this ListView activity once user successful login using tag "StartActivity(typeof(ListCompanies));"

Please suggest some sample code how to pass Service data into List View

Tagged:

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    edited March 18

    You can define a yourself Adapter which extends from BaseAdapter, in this adapter you can define your item layout and bind the Item data to Listview .There is a sample in the following you can refer to. And the whole sample is in the attachment.
    The main code is :

     private List<MovieModel> movies = new List<MovieModel>();
     private MovieAdapter movieAdapter;
    
    movieAdapter = new MovieAdapter(this,movies);
    listView.Adapter = movieAdapter;
    
    listView.ItemClick += ListView_ItemClick;
    private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        MovieModel movie = movies[e.Position];
       //.......      
    }
    

    MovieAdapter

    public class MovieAdapter : BaseAdapter<MovieModel>// the MovieModel is the binding Item Object of the ListView
    {
      private Context context;
      private List<MovieModel> movies;
    
     public MovieAdapter(Context context, List<MovieModel> movies)
    {
        this.context = context;
        this.movies = movies;
    }
    
    public override MovieModel this[int position] {
        get { return movies[position];  }
    }
    
    public override int Count {
        get { return movies.Count; }
    }
    
    public override long GetItemId(int position)
    {
        return position;
    }
    
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView; // re-use an existing view, if one is available
        MyViewHolder holder;
        if (view != null)
        {
            holder = view.Tag as MyViewHolder;
        }
        else
        { // otherwise create a new one
            holder = new MyViewHolder();
    
            LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
            view = inflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);
            holder.movieNameTV = view.FindViewById<TextView>(Android.Resource.Id.Text1);
            view.Tag = holder;
        }
    
        holder.movieNameTV.Text = movies[position].mMovieName;
        return view;
    }
    protected class MyViewHolder : Java.Lang.Object
    {
        public TextView movieNameTV { get; set; }
    }
    }
    

    For more details, you can check Populating a ListView With Data

    And there are some samples below the document, the link is :
    https://docs.microsoft.com/en-us/xamarin/android/user-interface/layouts/list-view/populating#related-links

  • sreedhar_msdaxsreedhar_msdax Member ✭✭

    Thanks Jezh,
    I will try this and will come back to you...:)

  • sreedhar_msdaxsreedhar_msdax Member ✭✭

    Hi Jezh,
    The supposed pattern is not working in my project. Because I have data in web service and I consumed it in Xamarin project
    And I am willing to show the service data in list view

  • jezhjezh Member, Xamarin Team Xamurai

    @sreedhar_msdax
    You just get the data list from the webservice, and then you can show the data in the listview. Any problem?

  • sreedhar_msdaxsreedhar_msdax Member ✭✭

    Hi Jezh,
    Thanks for quick reply..
    Yes I got struck at the time of using array adapter I can't able populate list view.If I hard code like this below

    compList = new List();

            compList.Add("CRONUS INDIA LTD");
            compList.Add("Test India 1");
            compList.Add("Test India 2");
    

    ArrayAdapter adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, compList);
    listView.Adapter = adapter;

    It is showing me result in list..But when I am running with service its not updating the code is below:

    NavSOAPService1.Companies compTablebuffer = new NavSOAPService1.Companies();

        Companies_Service companyList = new Companies_Service();
        {
            NetworkCredential credential = new NetworkCredential("administrator", "itreenav");
            //companyList.UseDefaultCredentials = true;
        }
    
        NavSOAPService1.Companies[] list = companyList.ReadMultiple(null, null, 0);
    
        //compList.Add(compTablebuffer.Name);
        foreach (NavSOAPService1.Companies comp in list)
        {
            compList.Add(comp.Name);
        }
    
        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, compList);
        listView.Adapter = adapter;
    

    Could you please suggest how to make it work..??

  • jezhjezh Member, Xamarin Team Xamurai

    @sreedhar_msdax
    It should has nothing to do with using array adapter. I guess when the code get to the following code ,

    ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, compList);
    listView.Adapter = adapter;
    

    The compList is still empty.
    The process of getting the data from webservice should be asynchronous, you should update the listview when you're done getting the data .

  • sreedhar_msdaxsreedhar_msdax Member ✭✭

    Hi Jezh,
    I have completed above issue now I am able to see list in emulator and one more suggestion required...on clicking any item from list another layout it should open another page how can I work with that, could you please suggest on the same

    listView.ItemClick += ListView_ItemClick;

        private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
          // What should be here 
        }
    

    async method I have tried but Navigation.PushAsync() is not working for me...is there any assembly should I need to give.?

  • sreedhar_msdaxsreedhar_msdax Member ✭✭

    Hi Jezh,
    Yes I am using Xamarin Android
    I will try this sample code and will get back to you

    Thanks and Regards
    Sree

  • sreedhar_msdaxsreedhar_msdax Member ✭✭

    Hi @Jezh,
    Thanks the suggestion worked perfectly and one more question if we want to filter from list view means, one page I have list of Warehouses and for each warehouse having different journal lines and those should show in another page listview. How can I acheive this, please suggest...

    Regards
    Sree

  • jezhjezh Member, Xamarin Team Xamurai

    @sreedhar_msdax
    Do you want to filter data from current ListView, and display them in another activity page?

  • sreedhar_msdaxsreedhar_msdax Member ✭✭

    @jezh
    almost like the same but next page also I have web service from there I need to retrieve data.

  • jezhjezh Member, Xamarin Team Xamurai

    @sreedhar_msdax
    If you want to filter data from current ListView, and display them in another activity page, you can pass the data from current page to another page by put the data in the Intent , then display the data.
    If you want to retrieve data from next page, you can get the data from the web service and display the data later.
    The sample I post previous display the dataList in a Listview, you can refer to it.

  • sreedhar_msdaxsreedhar_msdax Member ✭✭

    Hi @Jezh,
    Yes the approach is OK but requirement is in my current page I have warehouse list and now based on selected warehouse related Journals list need to appear now what happening is if I click on any warehouse its showing all Journals list

    In this case from where I need to track related info.

    Please suggest...

    Thanks & Regards
    Sree

  • jezhjezh Member, Xamarin Team Xamurai

    @sreedhar_msdax

    I don't know if I fully understand what you mean.

    When you click the warehouse list, you will get the information of selected warehouse , then you could get the related Journals list according the information of the selected warehouse.

    One case is to retrieve data by using web service Api according to the selected warehouse .
    The retrieving job is mainly in the web API on the back end.

    Another case is to get all Journals list first, and then retrieve data according to selected warehouse in mobile terminal, but this case is time-consuming and tedious, unless you don't have a lot of background data.

    So,it mainly depends on your actual needs and the web service Api to retrieve the data.
    Of course , I strongly recommend you to use the first method.

    Note: You can test the request web service Api by using PostMan.

  • sreedhar_msdaxsreedhar_msdax Member ✭✭

    Hi Jezh,
    Yeah what you understood is pretty clear. But the thing is I am not using API, my services are SOAP

    So how can achieve this, Please suggest some sample code..or any related reference link.,

    Thanks & Regards
    Sree

Sign In or Register to comment.