动态地创建和Android应用填充表 [英] Dynamically create and populate table in Android app

查看:139
本文介绍了动态地创建和Android应用填充表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何动态地创建和填充在Android上TableLayout。我可以创建表格事先静态(通过XML),但我需要能够从当服务器应用程序加载检索到的数据来填充它。列的数目将始终是相同的,但行数将根据检索到的数据集改变

I'm wondering how to dynamically create and populate a TableLayout in Android. I can create the table beforehand statically (via XML) but I need to be able to populate it with a dataset retrieved from a server when the application loads. The number of columns will always be the same but the number of rows will change depending on the dataset retrieved.

什么会去填充表的最佳方式?

What would be the best way to go about populating the table?

推荐答案

这是XML和code,有什么可能最终会在Xamarin.Android文件管理器的活动。在ListView由FileAdapter填充。该FileAdapter
保持FileSystemInfo对象的列表,这是很好的,因为的DirectoryInfo
和FileInfo的课程都是这种类型。你应该能够得到的要点
的一个ListView通过这个例子是如何工作的。我的例子是一个比较复杂
在起动的ListView你从Xamarin网站上看到的,因为列表视图
是不是显示器内唯一的视图。我有一些按钮沿水平
视图的顶部。这样用户就可以做额外的命令。我很抱歉,
我不能包含的图标。他们are't我的财产放弃。

This is the XML and code, for what could eventually be a File Manager Activity in Xamarin.Android. The ListView is populated by the FileAdapter. The FileAdapter keeps a list of FileSystemInfo objects, which is nice, because DirectoryInfo and FileInfo classes are both of this type. You should be able get the gist of how a ListView works via this example. My example is a bit more complicated than the starter listview you'd see from the Xamarin site, because the list view isn't the only view inside the display. I have some buttons horizontally along the top of the view. So that users can do extra commands.. I'm sorry that I can't include the icons. They are't my property to give away.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:background="@drawable/splash_image"
android:minHeight="25px">
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minWidth="25px"
    android:minHeight="25px">
    <ImageButton
        android:src="@drawable/undo"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/imageButton1" />
    <ImageButton
        android:src="@drawable/addfolder"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/imageButton3" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:minWidth="25px"
        android:minHeight="25px">
        <TextView
            android:id="@+id/folder"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dip"
            android:textColor="@android:color/white"
            android:textSize="20dip" />
    </LinearLayout>
</LinearLayout>
<ListView
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/list" />

[Activity (Label = "Documents")]            
public class DocumentsActivity : ListActivity
{
    string path;
    ListView ourlist;
    TextView folder;
    ImageButton back;
    ImageButton home;
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.DocumentsActivity);

        back = FindViewById<ImageButton> (Resource.Id.imageButton1);


        back.Click += delegate {
            this.BackClick();
        };
        back.LongClick += delegate {
            UserHelper.BackButton(this);
        };

        ourlist = FindViewById<ListView> (Android.Resource.Id.List);
        folder = FindViewById<TextView> (Resource.Id.folder);

        path = Android.OS.Environment.ExternalStorageDirectory.ToString();
        folder.Text = "Folder: "+new DirectoryInfo (path).Name;
        ourlist.Adapter = new FileAdapter (path, this);
    }
    private void CreateFolder(string path)
    {
        if (!Directory.Exists (path))
            Directory.CreateDirectory (path);
    }
    private void BackClick()
    {
        DirectoryInfo dir = new DirectoryInfo(path);
        ourlist.Adapter = new FileAdapter (dir.Parent.FullName, this);
        folder.Text = "Folder: "+dir.Parent.Name;

    } 
    protected override void OnListItemClick(ListView l, View v, int position, long id)
    {
        FileAdapter files = (FileAdapter)l.Adapter; 
        var t = files.Items[position];

        if (t is DirectoryInfo) {
        //Folder Behavior : 
            path = t.FullName;
            folder.Text = "Folder: "+t.Name;
            ourlist.Adapter = new FileAdapter (t.FullName,this);

        } else 
        {
        // File Behavior: (This is where will work will come in!)
            Android.Widget.Toast.MakeText (this, t.Name, Android.Widget.ToastLength.Short).Show ();
        }
    }
}



public class FileAdapter : BaseAdapter <FileSystemInfo>
{
    Activity _activity;
    string path;
    DirectoryInfo dir;

    public FileAdapter (string path,Activity act)
    {
        Items = new List<FileSystemInfo> ();
        dir = new DirectoryInfo (path);
        Items.AddRange (dir.GetDirectories().Where(z => !z.Name.StartsWith(".")).Cast<FileSystemInfo>().ToList());
        Items.AddRange (dir.GetFiles ());
        _activity = act;
    }

    public override FileSystemInfo this [int position] { 

        get { return Items[position]; }
    }
    public override int Count {
        get { return Items.Count() ; }
    }
    public override long GetItemId (int position)
    {
        return Items[position].GetHashCode();
    }

    public List<FileSystemInfo> Items {
        get;
        set;
    }

    public override View GetView (int position, View convertView, ViewGroup parent)
    {           
        View view = convertView; 

        if (view == null)
            view = _activity.LayoutInflater.Inflate (Resource.Layout.CustomView, null);

        if (Items [position] is DirectoryInfo) {
            ImageView imageview = view.FindViewById<ImageView> (Resource.Id.Image);
            imageview.SetImageResource (Resource.Drawable.Folder);
        } else {
            ImageView imageview = view.FindViewById<ImageView> (Resource.Id.Image);
            imageview.SetImageResource (Resource.Drawable.Files);
        }
        TextView text = view.FindViewById<TextView> (Resource.Id.Text1);
        text.Text = Items[position].Name;
        text.SetMinimumHeight (50);
        text.SetTextSize (Android.Util.ComplexUnitType.Pt, 10);

        return view;
    }
}

这篇关于动态地创建和Android应用填充表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆