我正在尝试设置一个带有2列的简单列表视图,我只想让它保存一个名称和年龄。我认为可能可以使用网格视图,但我不知道它需要保存多少名称,以及网格视图是否可以滚动。很抱歉,我的代码--我有一个列列表视图--可以用几种不同的方式很好地工作,而且现在到处都是。我只是在放点东西,这样你们就不会觉得我没试过了,我已经被困了一天半了,我什么也没做。我在任何地方都很难找到一个简单的多列列表视图示例的好例子。
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> 发布于 2015-05-22 00:24:16
我想你要找的是一个定制的ListView。它基本上是一个listView,但是您提供了自己的行布局(因此,可以更好地控制要显示多少列数据)以及您自己的BaseAdapter类。
下面是一个例子。
首先,您需要执行您的row_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>现在,您有一个行布局。我使用了weightSum属性,因为父布局的权重为100,并且每个textView (名称和年龄)都为50,因此您的列将被分割成一半。您可以更改textView的layout_weight,只要总数是LinearLayout的weightSum为100。
接下来,您需要创建一个BaseAdapter。这个类处理listView如何填充行布局。
创建一个新的类:
public class CustomListViewAdapterNameAndAge extends BaseAdapter {
private ArrayList<NameAndAgeClass> listData;
private LayoutInflater layoutInflater;
private Context context;
public CustomListViewAdapterNameAndAge(Context context, ArrayList<NameAndAgeClass> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// int type = getItemViewType(position);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.row_layout.xml, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.nameTextView);
holder.age = (TextView) convertView.findViewById(R.id.ageTextView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(listData.get(position).getName);
holder.age.setText(listData.get(position).getAge));
return convertView;
}
static class ViewHolder {
TextView name;
TextView age;
}
}它可能看起来像一口,但我会为你打破它。重要的部分是getView函数,其中您获取布局文件row_layout.xml,然后获取您的holder对象(参见下面的ViewHolder ),根据您的布局文件设置名称和年龄TextViews,然后根据您的listData ArrayList设置值。NameAndAgeClass对象是用于填充列表的对象。在您的示例中,它将name和age作为私有变量,然后将有setter、getters和构造函数。我想你已经知道怎么做了,所以我不会再把代码放进去了。
在您的主要活动中,在您的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);就是这样。您的应用程序应该在带有2列的ListView中显示您的数据。
之后,您可以向您的onItemClickedListener添加一个listView。
https://stackoverflow.com/questions/30386271
复制相似问题