Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ListView仅显示arraylist中的第一项_Java_Android_Android Studio_Android Listview_Custom Arrayadapter - Fatal编程技术网

Java ListView仅显示arraylist中的第一项

Java ListView仅显示arraylist中的第一项,java,android,android-studio,android-listview,custom-arrayadapter,Java,Android,Android Studio,Android Listview,Custom Arrayadapter,以前,由于对资源文件的错误引用,我遇到了崩溃问题。修复了该问题,并用我得到的逻辑错误更新了该线程。 我是android新手,目前正在学习自定义类和适配器。工作时,我面临一个问题,即listview仅显示第一个arraylist项。 我还附上了所需文件的代码 工作活动 package np.com.shresthakiran.tourswoniga; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle

以前,由于对资源文件的错误引用,我遇到了崩溃问题。修复了该问题,并用我得到的逻辑错误更新了该线程。
我是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);

    }
}
列表视图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 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>

列出行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 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>

列表视图
仅显示第一项是因为您已将
heritages\u行
布局中的高度设置为
match\u parent
,这将覆盖整个屏幕高度,对于下一项,您必须向下滚动,即使第一项的内容没有覆盖整个高度


要使每一行仅覆盖其显示的内容,请在ListView布局文件中使用
wrap\u content
而不是
match\u parent

,我犯了一个错误。我写了android:layout\u height=“wrap\u content”,实际上应该是android:layout\u height=“match\u parent”。将ListView XML中的android:layout\u height=“wrap\u content”更改为android:layout\u height=“match\u parent”解决了这个问题。