Android 多栏列表视图

Android 多栏列表视图,android,listview,Android,Listview,我正在为我的应用程序创建排行榜、高分列表。我想在4列中显示数据,排名、姓名、创建者、分数,并通过对象列表相应地填充它们。我想在列表上一次显示5个项目,总共20个项目,使列表可以滚动。我找不到一个合适的方法来做这件事。这将是伟大的,如果我能得到一个快速的指导“如何”,因为我的工作的最后期限快到了 非常感谢:)您可以通过定义自定义ListView轻松完成这项工作 要定义自定义listview,只需以水平方式定义一个带有4个textview的自定义行布局文件。现在,在listview的自定义适配器中扩

我正在为我的应用程序创建排行榜、高分列表。我想在4列中显示数据,排名、姓名、创建者、分数,并通过对象列表相应地填充它们。我想在列表上一次显示5个项目,总共20个项目,使列表可以滚动。我找不到一个合适的方法来做这件事。这将是伟大的,如果我能得到一个快速的指导“如何”,因为我的工作的最后期限快到了


非常感谢:)

您可以通过定义自定义ListView轻松完成这项工作

要定义自定义listview,只需以水平方式定义一个带有4个textview的自定义行布局文件。现在,在listview的自定义适配器中扩展该布局文件,为此,需要重写getView()方法并扩展该行布局文件

更新: 只需选中此项即可定义自定义listview,但请确保通过使用4个水平textview定义自定义行布局文件来使用本教程

以下是row\u layout.xml文件:

<LinearLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/FirstText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="First"
        android:layout_weight="1">
    </TextView>

    <TextView
        android:id="@+id/SecondText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Second"
        android:layout_weight="1">
    </TextView>

    <TextView
        android:id="@+id/ThirdText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Third"
        android:layout_weight="1">
    </TextView>

    <TextView
        android:id="@+id/FourthText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Fourth"
        android:layout_weight="1">
    </TextView>
</LinearLayout>

通过定义自定义ListView,您可以轻松完成这项工作

要定义自定义listview,只需以水平方式定义一个带有4个textview的自定义行布局文件。现在,在listview的自定义适配器中扩展该布局文件,为此,需要重写getView()方法并扩展该行布局文件

更新: 只需选中此项即可定义自定义listview,但请确保通过使用4个水平textview定义自定义行布局文件来使用本教程

以下是row\u layout.xml文件:

<LinearLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/FirstText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="First"
        android:layout_weight="1">
    </TextView>

    <TextView
        android:id="@+id/SecondText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Second"
        android:layout_weight="1">
    </TextView>

    <TextView
        android:id="@+id/ThirdText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Third"
        android:layout_weight="1">
    </TextView>

    <TextView
        android:id="@+id/FourthText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Fourth"
        android:layout_weight="1">
    </TextView>
</LinearLayout>


列表视图中的每个项目都从布局文件中膨胀。在布局文件中,可以添加四个水平对齐的textview。有关更多信息,您需要关注EfficientAdapter,它会导致自定义ListView。

ListView中的每个项目都是从布局文件中膨胀出来的。在布局文件中,可以添加四个水平对齐的textview。要了解更多信息,您需要关注EfficientAdapter,它将导致自定义ListView。

最终找到了一个解决方案。我和一个刚接触安卓系统的人以及安卓系统的Xamarin有同样的问题。要明确的是,我使用的是Xamarin Studio,而Xamarin是用C#编写的Android

尽管SimpleCursorAdapter似乎可以容纳多个字段,但当绑定到SimpleListItem之类的对象时,只会使用一个字段

我第一次尝试这个:

        string[] fromColumns = new string[]{ "checkListName","checkListDesc" };
        int[] toControlIDs = new int[] {Android.Resource.Id.Text1, Android.Resource.id.Text1};

        try{
            listView.Adapter = new SimpleCursorAdapter(this,Android.Resource.Layout.SimpleListItem1 , c, fromColumns, toControlIDs, 0);         
        }
        catch(SQLiteException e){
            Console.WriteLine ("whoops, " + e.Message.ToString ());
        }
但我得到的只是光标行中的最后一个字段

我了解到需要一个自定义列表视图。以下是四个文件的代码文件:

  • MainActivity.cs
  • myList.xml
  • Main.axml
  • PreFloat.cs(这是数据库部分)
  • 我已经包括了所有这些,以提供尽可能多的一个完整的工作样本,尽可能接近现实生活

    下面是MainActivity.cs,它设置内容视图,使用光标从SQLite数据库获取数据,并定义

    using System;
    
    using Android.App;
    using Android.Content;
    using Android.Database;
    using Android.Database.Sqlite;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    
    namespace Darjeeling
    {
    [Activity (Label = "Darjeeling", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {       
    
        ListView listView;
        Darjeeling.PreFloatDatabase pdb;
        ICursor c;
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
    
            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            listView = FindViewById<ListView> (Resource.Id.listView1);
            pdb = new PreFloatDatabase (this);
            //  Assign the cursor to a query
            c = pdb.ReadableDatabase.RawQuery ("select * from checkLists", null);
            StartManagingCursor (c);
            // A ListView needs an adapter -- so we'll assign our instantiated listView's adapter to our customized adapter called HomeScreenCursorAdapter.
            listView.Adapter = (IListAdapter)new HomeScreenCursorAdapter (this,c);
        }
        //  End onCreate method
    
        // This handles the cursor when the user is done with the activity
        protected override void OnDestroy()
        {
            StopManagingCursor(c);
            c.Close ();
            base.OnDestroy();
        }
        //  Here's the magic -- 
        public class HomeScreenCursorAdapter : CursorAdapter {
            Activity context;
            public HomeScreenCursorAdapter(Activity context, ICursor c)
                : base (context, c)
            {
                this.context = context;
            }
            //  This overridden BindView method is going to let me assign TextView controls that I've set up in an XML file, to specific fields in the cursor.  
            public override void BindView(View view, Context context, ICursor cursor)
            {
                var txtCheckListName = view.FindViewById<TextView> (Resource.Id.txtCheckListName); //(Android.Resource.Id.Text1);
                var txtCheckListDesc = view.FindViewById<TextView> (Resource.Id.txtCheckListDesc); //(Android.Resource.Id.Text2); 
    //  For testing purposes, I first assigned static values to txtCheckListName and txtCheckListDesc, for instance, txtCheckListName.Text = "Hello";  and txtCheckListDesc.Text = "World"; 
                txtCheckListName.Text = cursor.GetString (3);
                txtCheckListDesc.Text = cursor.GetString (4);
            }
            //  This overridden View inflates each row (I think).  This could inflate a built-in ListView control like SimpleListItem, OR, in this case, it references a custom written XML file called myList.
            public override View NewView(Context context, ICursor cursor, ViewGroup parent)
            {
                return this.context.LayoutInflater.Inflate (Resource.Layout.myList, parent, false);
            }
        }
    }
    
    问题: SimpleListItem控件应该有多个字段还是最多有一个复选框控件和标签? 是否应该改用网格控件

    备选方案: 使用SQL简单地连接所需的值不是更容易,而不是进行所有这些恶作剧吗?特别是因为协调两个文本控件的位置可能太复杂了

    全面披露:
    这段代码是我在其他帖子和论坛上读到的代码的混合体,并根据我自己的特殊要求进行了定制

    终于找到了解决办法。我和一个刚接触安卓系统的人以及安卓系统的Xamarin有同样的问题。要明确的是,我使用的是Xamarin Studio,而Xamarin是用C#编写的Android

    尽管SimpleCursorAdapter似乎可以容纳多个字段,但当绑定到SimpleListItem之类的对象时,只会使用一个字段

    我第一次尝试这个:

            string[] fromColumns = new string[]{ "checkListName","checkListDesc" };
            int[] toControlIDs = new int[] {Android.Resource.Id.Text1, Android.Resource.id.Text1};
    
            try{
                listView.Adapter = new SimpleCursorAdapter(this,Android.Resource.Layout.SimpleListItem1 , c, fromColumns, toControlIDs, 0);         
            }
            catch(SQLiteException e){
                Console.WriteLine ("whoops, " + e.Message.ToString ());
            }
    
    但我得到的只是光标行中的最后一个字段

    我了解到需要一个自定义列表视图。以下是四个文件的代码文件:

  • MainActivity.cs
  • myList.xml
  • Main.axml
  • PreFloat.cs(这是数据库部分)
  • 我已经包括了所有这些,以提供尽可能多的一个完整的工作样本,尽可能接近现实生活

    下面是MainActivity.cs,它设置内容视图,使用光标从SQLite数据库获取数据,并定义

    using System;
    
    using Android.App;
    using Android.Content;
    using Android.Database;
    using Android.Database.Sqlite;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    
    namespace Darjeeling
    {
    [Activity (Label = "Darjeeling", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {       
    
        ListView listView;
        Darjeeling.PreFloatDatabase pdb;
        ICursor c;
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
    
            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            listView = FindViewById<ListView> (Resource.Id.listView1);
            pdb = new PreFloatDatabase (this);
            //  Assign the cursor to a query
            c = pdb.ReadableDatabase.RawQuery ("select * from checkLists", null);
            StartManagingCursor (c);
            // A ListView needs an adapter -- so we'll assign our instantiated listView's adapter to our customized adapter called HomeScreenCursorAdapter.
            listView.Adapter = (IListAdapter)new HomeScreenCursorAdapter (this,c);
        }
        //  End onCreate method
    
        // This handles the cursor when the user is done with the activity
        protected override void OnDestroy()
        {
            StopManagingCursor(c);
            c.Close ();
            base.OnDestroy();
        }
        //  Here's the magic -- 
        public class HomeScreenCursorAdapter : CursorAdapter {
            Activity context;
            public HomeScreenCursorAdapter(Activity context, ICursor c)
                : base (context, c)
            {
                this.context = context;
            }
            //  This overridden BindView method is going to let me assign TextView controls that I've set up in an XML file, to specific fields in the cursor.  
            public override void BindView(View view, Context context, ICursor cursor)
            {
                var txtCheckListName = view.FindViewById<TextView> (Resource.Id.txtCheckListName); //(Android.Resource.Id.Text1);
                var txtCheckListDesc = view.FindViewById<TextView> (Resource.Id.txtCheckListDesc); //(Android.Resource.Id.Text2); 
    //  For testing purposes, I first assigned static values to txtCheckListName and txtCheckListDesc, for instance, txtCheckListName.Text = "Hello";  and txtCheckListDesc.Text = "World"; 
                txtCheckListName.Text = cursor.GetString (3);
                txtCheckListDesc.Text = cursor.GetString (4);
            }
            //  This overridden View inflates each row (I think).  This could inflate a built-in ListView control like SimpleListItem, OR, in this case, it references a custom written XML file called myList.
            public override View NewView(Context context, ICursor cursor, ViewGroup parent)
            {
                return this.context.LayoutInflater.Inflate (Resource.Layout.myList, parent, false);
            }
        }
    }
    
    问题: SimpleListItem控件应该有多个字段还是最多有一个复选框控件和标签? 是否应该改用网格控件

    备选方案: 使用SQL简单地连接所需的值不是更容易,而不是进行所有这些恶作剧吗?特别是因为协调两个文本控件的位置可能太复杂了

    全面披露:
    这段代码是我在其他帖子和论坛上读到的代码的混合体,并根据我自己的特殊要求进行了定制

    你能给我链接一篇文章或一个合适的教程吗?你的方法看起来很可靠,我很愿意学习,但是膨胀没有意义,是一种方法还是什么?谢谢。。我刚刚读完并理解了博客,现在就开始尝试实现它。@MohammadUmairKhan这太棒了,创建自定义适配器是一次性的实践。所以你要确保你做得很完美。。帕雷什。。你让我的日子过得很轻松!!感谢无数[对不起,我很高兴:)]你能给我链接一篇文章或一个合适的教程吗?你的方法看起来很可靠,我愿意学习,但是膨胀没有意义,是一种方法还是什么?谢谢。。我刚刚读完并理解了博客,现在就开始尝试实现它。@MohammadUmairKhan这太棒了,创建自定义适配器是一次性的实践。所以你要确保你做得很完美