如何在ListView中填充行?

时间:2016-05-23 11:48:44

标签: android xamarin

所以,标题说得很好。我希望能够连续写入数据。然而,这对我来说是一个问题,因为我不知道在哪里以及如何放入使这成为可能的代码。我正在使用Xamarin,这是我的代码;

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; 


        }
    }
}

这是来自MainActivity.cs

1 个答案:

答案 0 :(得分:1)

这是Listview的演示

以下是主要活动文件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);
   }
}

以下是res / layout / activity_main.xml文件的内容

<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>

以下将是res / values / strings.xml的内容来定义两个新常量

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

以下是res / layout / activity_listview.xml文件的内容

<?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>