繁体   English   中英

ListView 只显示数组列表中的第一项

[英]ListView only shows the first item from the arraylist

以前,由于对资源文件的错误引用,我遇到了崩溃问题。 修复了该问题并使用我得到的逻辑错误更新了此线程。
我是 android 新手,目前正在学习自定义类和适配器。 在工作时,我面临一个问题,即列表视图仅显示第一个数组列表项。 我也附上了所需文件的代码。

工作活动

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;
    }
}

列表视图 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_width="match_parent"
android:layout_height="100dp"
android:minHeight="100dp">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="100"
    android:layout_margin="10dp">

    <ImageView
        android:id="@+id/ivHeritage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        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_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="66.66"
        android:padding="8dp" >

        <TextView
            android:id="@+id/tvHeritageName"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            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_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="18sp"
            android:text="Taumadi"
            android:padding="2dp"/>

    </LinearLayout>
</LinearLayout>

listview显示的第一个项目不仅是因为你必须在设定的高度heritages_row布局match_parent将覆盖整个屏幕的高度,为下一个项目,你向下滚动,即使第一项的内容不覆盖整个高度.

要使每一行仅覆盖其显示的内容,请使用wrap_content而不是match_parent

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM