簡體   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