gpt4 book ai didi

android如何创建多行 ListView

转载 作者:行者123 更新时间:2023-11-29 17:58:08 25 4
gpt4 key购买 nike

我正在尝试创建一个列表项,该列表项由两个列表项组成。我使用的代码是这样的:

package com.example.list2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv1=(ListView)findViewById(R.id.listView1);
Level data[] = new Level[]
{
new Level("Heading 1", "Subheading 1"),
new Level("Heading 2", "Subheading 2"),
new Level("Heading 3", "Subheading 3")
};
LevelAdapter adp=new LevelAdapter(this, R.layout.list_item, data);
lv1.setAdapter(adp);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

level.java 文件是这样的:

package com.example.list2;

public class Level {
//public int icon;
public String title;
public String title2;

public Level()
{
super();
}

public Level(String title,String title2) {
super();
//this.icon = icon;
this.title = title;
this.title2=title2;
}

}

leveladapter是这个

package com.example.list2;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LevelAdapter extends ArrayAdapter<Level> {

static Context context;
static int layoutResourceId;
Level data[] = null;

public LevelAdapter(Context context, int layoutResourceId, Level[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}


public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;

if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
//row.setMinimumHeight(200);
holder = new WeatherHolder();
// holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.txtTitle2 = (TextView)row.findViewById(R.id.txtTitle2);

row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}

Level weather = data[position];
holder.txtTitle.setText(weather.title);
// holder.imgIcon.setImageResource(weather.icon);

return row;
}

static class WeatherHolder
{
// ImageView imgIcon;
TextView txtTitle;
TextView txtTitle2;
// ImageView imgIcon2;
}

}

列表项布局是这样的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">



<TextView android:id="@+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="22sp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:padding="50dip"
android:textColor="#736F6E"
android:layout_alignParentLeft="true"
android:textAppearance="@android:attr/textAppearanceLarge"
/>
<TextView
android:id="@+id/txtTitle2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_below="@id/txtTitle"
android:textStyle="bold"
android:textSize="12sp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:padding="50dip"
android:textColor="#736F6E"
android:layout_alignParentLeft="true"
android:textAppearance="@android:attr/textAppearanceLarge"
/>

</RelativeLayout>

最后主要 Activity 的布局是这样的

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


<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:dividerHeight="1dp"

/>

</RelativeLayout>

在输出中,我得到的只是一个高度非常大的列表项,但只有显示标题而不是副标题。我哪里错了?谢谢

最佳答案

好吧,我将列表项 XML 维度缩小为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >

<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:gravity="center_vertical"
android:padding="2dip"
android:textAppearance="@android:attr/textAppearanceLarge"
android:textColor="#736F6E"
android:textSize="22sp"
android:textStyle="bold" />

<TextView
android:id="@+id/txtTitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/txtTitle"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:gravity="center_vertical"
android:padding="2dip"
android:textAppearance="@android:attr/textAppearanceLarge"
android:textColor="#736F6E"
android:textSize="12sp"
android:textStyle="bold" />

</RelativeLayout>

LevelAdapter#getView 中,就在 return row 之前,我添加了:holder.txtTitle2.setText(weather.title2);

结果如下图文件: Result image after below two changes

这就是您要实现的目标吗?我没有提到从 LevelAdapter 中删除 static 字段的其他改进。

关于android如何创建多行 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17788776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com