ListView 仅显示数组列表中的第一项
Posted
技术标签:
【中文标题】ListView 仅显示数组列表中的第一项【英文标题】:ListView only shows the first item from the arraylist 【发布时间】:2021-02-13 12:28:59 【问题描述】:以前,由于对资源文件的错误引用,我遇到了崩溃问题。修复了这个问题,并用我得到的逻辑错误更新了这个线程。 我是 android 新手,目前正在学习自定义类和适配器。在工作时,我遇到了一个问题,即 listview 仅显示第一个 arraylist 项。 我也附上了所需文件的代码。
工作活动
package np.com.shresthakiran.tourswoniga;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class KhowpaActivity extends AppCompatActivity
ListView lvHeritageList;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_heritage);
lvHeritageList = findViewById(R.id.lvHeritage);
ArrayList<Heritages> heritageAryList = new ArrayList<>();
heritageAryList.add(new Heritages(R.drawable.ic_launcher_background,"Ngatapol", "Taumadi"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "Dattatreya", "Taumadi"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "Lu dhwakha", "Lyaaku"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "55 jhyale Durbar", "Lyaaku"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "Taleju Bhawani", "Lyaaku"));
HeritageAdapter heritageAdapter = new HeritageAdapter(KhowpaActivity.this, R.layout.heritages_row, heritageAryList);
lvHeritageList.setAdapter(heritageAdapter);
自定义适配器
package np.com.shresthakiran.tourswoniga;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class HeritageAdapter extends ArrayAdapter<Heritages>
private Context mContext;
private int mResource;
public HeritageAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Heritages> objects)
super(context, resource, objects);
this.mContext = context;
this.mResource = resource;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
LayoutInflater layoutInflater =LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(mResource, parent, false);
ImageView ivHeritageImage = convertView.findViewById(R.id.ivHeritage);
TextView tvHeritageName = convertView.findViewById(R.id.tvHeritageName);
TextView tvHeritageAddress = convertView.findViewById(R.id.tvHeritageAddress);
ivHeritageImage.setImageResource(getItem(position).getmImageResourceId());
tvHeritageName.setText(getItem(position).getmHeritageName());
tvHeritageAddress.setText(getItem(position).getmHeritageAddress());
return convertView;
对象类
package np.com.shresthakiran.tourswoniga;
public class Heritages
private int mImageResourceId;
private String mHeritageName;
private String mHeritageAddress;
public Heritages(int heritageImageResourceId, String heritageName, String heritageAddress)
this.mImageResourceId = heritageImageResourceId;
this.mHeritageName = heritageName;
this.mHeritageAddress = heritageAddress;
public int getmImageResourceId()
return mImageResourceId;
public String getmHeritageName()
return mHeritageName;
public String getmHeritageAddress()
return mHeritageAddress;
ListView XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolbar"
android:layout_
android:layout_
android:minHeight="100dp">
<ListView
android:layout_
android:layout_
android:id="@+id/lvHeritage">
</ListView>
</RelativeLayout>
列表行 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="horizontal"
android:weightSum="100"
android:layout_margin="10dp">
<ImageView
android:id="@+id/ivHeritage"
android:layout_
android:layout_
android:layout_weight="33.33"
android:padding="2dp"
android:text="Ngatapol"
android:layout_marginTop="7dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:id="@+id/llHeritageInfo"
android:layout_
android:layout_
android:orientation="vertical"
android:layout_weight="66.66"
android:padding="8dp" >
<TextView
android:id="@+id/tvHeritageName"
android:layout_
android:layout_
android:layout_weight="1"
android:textSize="18sp"
android:textStyle="bold"
android:text="Ngatapol"
android:textAppearance="?android:textAppearanceMedium"
android:padding="2dp"/>
<TextView
android:id="@+id/tvHeritageAddress"
android:layout_
android:layout_
android:layout_weight="1"
android:textSize="18sp"
android:text="Taumadi"
android:padding="2dp"/>
</LinearLayout>
</LinearLayout>
【问题讨论】:
在 ListView 布局文件中,我犯了一个错误。我写了 android:layout_ 实际上应该是 android:layout_ 。在 ListView XML 中将 android:layout_ 更改为 android:layout_ 解决了这个问题。 【参考方案1】:listview
仅显示第一项是因为您已将heritages_row
布局中的高度设置为match_parent
,这将覆盖整个屏幕高度,而对于下一项,您必须向下滚动,即使内容第一项没有覆盖整个高度。
要使每一行只覆盖其显示的内容,请使用wrap_content
而不是match_parent
。
【讨论】:
以上是关于ListView 仅显示数组列表中的第一项的主要内容,如果未能解决你的问题,请参考以下文章
使用 Cursor Loader 选择 ListView 中的第一项