Android 有两列的简单列表?

Android 有两列的简单列表?,android,listview,android-listview,Android,Listview,Android Listview,我正在尝试设置一个包含两列的简单listview,我只希望它包含一个名称和年龄。我认为可能可以使用网格视图,但我不知道它需要保存多少名称,以及网格视图是否可滚动。我对我的代码表示歉意,我有1列列表视图,可以通过几种不同的方式很好地工作,现在我到处都是。我只是放了些东西,你们不要以为我没试过,我已经被困了一天半了,我什么都没得到。我很难在任何地方找到一个简单的多列listview示例的好示例 ArrayAdapter adapter = new ArrayAdapter<Strin

我正在尝试设置一个包含两列的简单listview,我只希望它包含一个名称和年龄。我认为可能可以使用网格视图,但我不知道它需要保存多少名称,以及网格视图是否可滚动。我对我的代码表示歉意,我有1列列表视图,可以通过几种不同的方式很好地工作,现在我到处都是。我只是放了些东西,你们不要以为我没试过,我已经被困了一天半了,我什么都没得到。我很难在任何地方找到一个简单的多列listview示例的好示例

     ArrayAdapter adapter = new ArrayAdapter<String>(this,
              R.layout.list11, minu);

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









<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
     android:weightSum="100"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >




    <LinearLayout
        android:id="@+id/fskdumnn"
         android:orientation="horizontal"
         android:background="#CC0000"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

     </LinearLayout>




     <LinearLayout
        android:id="@+id/fsvumnn"
         android:orientation="horizontal"
         android:background="#CC0000"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6" >



            <TextView
            android:id="@+id/tekxw1"
            android:layout_width="0dp"

            android:layout_weight="2"
            android:layout_height="match_parent"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />




         <TextView
            android:id="@+id/jmnxx"
            android:layout_width="0dp"
            android:textStyle="bold|italic"
            android:shadowDx="1.6"
            android:shadowDy="1.6"
            android:gravity="center_vertical|center_horizontal"
            android:shadowRadius="2.0"
            android:shadowColor="#FFADD6"
            android:layout_weight="44"
            android:layout_height="match_parent"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceLarge" />


         <TextView
            android:id="@+id/jcxxx"
            android:layout_width="0dp"
             android:gravity="center_vertical|center_horizontal"
            android:layout_weight="44"
            android:layout_height="match_parent"
            android:text="Age"
            android:textAppearance="?android:attr/textAppearanceLarge" />


  <TextView
            android:id="@+id/jcfxx"
            android:layout_width="0dp"

            android:layout_weight="2"
            android:layout_height="match_parent"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />

 </LinearLayout>


      <LinearLayout
        android:id="@+id/fhfdmnn"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="90"
        android:background="#ED69AB"
        android:orientation="horizontal"
        android:weightSum="100" >


        <TextView
            android:id="@+id/jmacxsnxx"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            android:text=""
           android:background="#CC0000"
            android:textAppearance="?android:attr/textAppearanceLarge" />


           <ListView
           android:id="@+id/country_list"
           android:layout_weight="96"
           android:layout_width="0dp"
           android:layout_height="wrap_content" >
          </ListView>




     <TextView
            android:id="@+id/jmbfcnxx"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            android:text=""
            android:background="#CC0000"
            android:textAppearance="?android:attr/textAppearanceLarge" />




     </LinearLayout>


    </LinearLayout>  
ArrayAdapter=新的ArrayAdapter(此,
R.layout.list11,分钟);
ListView ListView=(ListView)findViewById(R.id.country\u列表);
setAdapter(适配器);

我想你要找的是一个定制的ListView。它基本上是一个listView,但您可以提供自己的行布局(因此,可以更好地控制要显示多少列数据),以及自己的
BaseAdapter

这里有一个例子

首先,您需要执行
row\u layout.xml
(在布局文件夹中创建此文件)文件,因为您希望每一行显示名称和年龄,所以它看起来是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="100" >

    <TextView
        android:id="@+id/nameTextView"
        android:layout_gravity="center"
        android:layout_width="0dp"
        android:layout_weight="50"
        android:layout_height="match_parent"
        android:textSize="25dp"
        android:text="Name" />

    <TextView
        android:id="@+id/ageTextView"
        android:layout_gravity="center"
        android:layout_width="0dp"
        android:layout_weight="50"
        android:layout_height="match_parent"
        android:textSize="25dp"
        android:text="Age" />

</LinearLayout>
它看起来像一口,但我会给你分解。重要的部分是
getView
函数,在该函数中,您可以获取布局文件
row\u layout.xml
,然后获取holder对象(请参见下面的ViewHolder),根据布局文件设置名称和年龄文本视图,然后根据
listData
ArrayList设置值。NameAndAgeClass对象是用于填充列表的对象。在您的例子中,它将有
name
age
作为私有变量,然后您将有setter、getter和构造函数。我想你已经知道怎么做了,所以我不会再写代码了

在主要活动中,在
onCreate()
函数中,执行以下操作:

//instantiate your listView
ListView nameAndAgeListView = (ListView) findViewById(R.id.idOfYourListViewHere);

//create your listView with your custom object
ArrayList<NameAndAgeClass> nameAndAgeList = new ArrayList<>();

//
    IMPT, POPULTE THE ARRAYLIST HERE
    you can do a for loop or whatever you want
//

//create your adapter, use the nameAndAgeList ArrayList
CustomListViewAdapterNameAndAge nameAndAgeAdapter = new CustomListViewAdapterNameAndAge(this, nameAndAgeList);

//get your listView and use your adapter
nameAndAgeListView.setAdapter(nameAndAgeAdapter);
//实例化您的listView
ListView名称和GelistView=(ListView)findViewById(R.id.idOfYourListViewHere);
//使用自定义对象创建listView
ArrayList name和agelist=新的ArrayList();
//
IMPT,在这里宣传ARRAYLIST
你可以做一个for循环或者你想做的任何事情
//
//创建适配器时,请使用nameAndAgeList ArrayList
CustomListViewAdapterMeanidage name和geAdapter=新的CustomListViewAdapterMeanidage(该名称和标签);
//获取listView并使用适配器
setAdapter(nameAndAgeAdapter);
就这样。你的应用程序应该在一个包含两列的列表视图中显示你的数据

之后,您可以向listView添加一个
onItemClickedListener


GridView是一个视图组,以二维可滚动网格显示项目@Sharj呃没有?他正在寻找一个自定义列表视图。查一查。我认为定制的listView是您需要的。与ListView相比,它的工作量要大一点,但它可以帮您完成任务。@Sharj谢谢,我将查看网格视图,以前从未使用过它。@Razgriz它对我来说并不重要,只要它是可滚动的,有两列,并且可以单击,我想它对我来说是可行的。只想显示姓名和年龄之类的内容,让他们点击它。@user3705179好的,我现在写一个答案。希望它能帮到你。只要问一下你是否需要澄清什么,我会很乐意帮你。我甚至不知道该问什么问题,哈哈,从编译到编译,我都有20个错误,别担心,我不是你!希望我能弄明白。我只想给大家一点,以防有人回答,如果没有,我会接受你的回答,我感谢你的尝试。我可以问一个编译的例子吗,很抱歉,我问了,但我太迷茫了,我认为你过度估计了我的能力。哈哈,我从来没有真正与ListView合作过。即使它只有两个条目,菲尔23和鲍勃34。Idk这对你来说是多少工作。我很抱歉让你感到痛苦,但我觉得如果我问你每一个错误,我想我会在你这方面做更多的工作,我认为这对其他遇到这一点的人来说是好的,只是有一个很好的例子,他们也可以修补。如果不是很大的痛苦。泰!!!如果我只是编辑我的答案,让它更直截了当,可以吗?是的,当然,无论什么对你来说是最好的,我真的很感激,对不起,我知道这不是你,这是我缺乏理解!
//instantiate your listView
ListView nameAndAgeListView = (ListView) findViewById(R.id.idOfYourListViewHere);

//create your listView with your custom object
ArrayList<NameAndAgeClass> nameAndAgeList = new ArrayList<>();

//
    IMPT, POPULTE THE ARRAYLIST HERE
    you can do a for loop or whatever you want
//

//create your adapter, use the nameAndAgeList ArrayList
CustomListViewAdapterNameAndAge nameAndAgeAdapter = new CustomListViewAdapterNameAndAge(this, nameAndAgeList);

//get your listView and use your adapter
nameAndAgeListView.setAdapter(nameAndAgeAdapter);