How do I populate a row in ListView?

So, the title says it pretty well. I want to be able to write data in a row. However this has been a problem for me, as I'm not sure where and how to put in the code that would make this possible. I'm working with Xamarin and here's my code;

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;

namespace SAVEeddit.droid
{
    [Activity(Label = "SAVEeddit.droid", MainLauncher = true, Icon =        "@drawable/icon")]
    public class MainActivity : Activity
    {
        //int count = 1;
        private List<string> mItems;
        private ListView mListView; 

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            mListView = FindViewById<ListView>(Resource.Id.listView1);

            mItems = new List<string>();
            mItems.Add("Trainer 1");
            mItems.Add("Trainer 2");
            mItems.Add("Trainer 3");
            mItems.Add("Trainer 4");
            mItems.Add("Trainer 5");
            mItems.Add("Trainer 6");

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


        }
    }
}

This is from MainActivity.cs

Upvotes: 0

Views: 88

Answers (1)

kunal
kunal

Reputation: 328

Here is the Demo for Listview

Following is the content of the main activity file src/com.example.ListDisplay/ListDisplay.java

package com.example.ListDisplay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListDisplay extends Activity {
   // Array of strings...
   String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};

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

      ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);

      ListView listView = (ListView) findViewById(R.id.mobile_list);
      listView.setAdapter(adapter);
   }
}

Following will be the content of res/layout/activity_main.xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".ListActivity" >

   <ListView
      android:id="@+id/mobile_list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
   </ListView>

</LinearLayout>

Following will be the content of res/values/strings.xml to define two new constants

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">ListDisplay</string>
   <string name="action_settings">Settings</string>
</resources>

Following will be the content of res/layout/activity_listview.xml file:

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/label"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="10dip"
   android:textSize="16dip"
   android:textStyle="bold" >
</TextView>

Upvotes: 1

Related Questions