有人能向我展示为什么ListView没有显示吗?

huangapple go评论71阅读模式
英文:

Can somebody show me why ListView isn't showing?

问题

以下是翻译后的内容:

以下项目是从在线课程中逐字复制的。它应该显示一个 ListView,但什么都没有显示出来。我对 Android Studio 还完全不熟,这只是我在其中的第二个项目。总共有 5 个文件,我将逐个显示它们的内容。我只需要另一双或两双眼睛来找出问题,因为我“对错误视而不见”。任何帮助将不胜感激。

activity_main.xml:

    <androidx.constraintlayout.widget.ConstraintLayout 
    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"
        tools:context=".MainActivity">
    
    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:

    package com.example.tadhg.uiuxadapterpractical;

    import androidx.appcompat.app.AppCompatActivity;

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.os.Bundle;
    import android.widget.ListView;

    public class MainActivity extends AppCompatActivity {

      ListView lv;
      Context context;

      TypedArray images;
      String [] titleList;
      String [] descList;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          context = this;

          titleList = getResources().getStringArray(R.array.Languages);
          descList = getResources().getStringArray(R.array.LanguageDesc);
          images = getResources().obtainTypedArray(R.array.LanguageImg);

          lv = (ListView) findViewById(R.id.listView);

        }
    }

OurAdapter.java:

    package com.example.tadhg.uiuxadapterpractical;

    import android.content.Context; 
    import android.content.res.TypedArray;
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.BaseAdapter; 
    import android.widget.ImageView; 
    import android.widget.TextView;

    public class OurAdapter extends BaseAdapter {

        private String [] titles;
        private String [] desc;
        private TypedArray imageID;

        private Context context;
        private static LayoutInflater layoutInflater;

        //constructor
        OurAdapter(Context adapterContext, String[] titleList, String[] descList, TypedArray images){
            titles = titleList;
            desc = descList;
            imageID = images;
            context = adapterContext;

            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        private class ViewHolder {
            TextView tv1, tv2;
            ImageView img;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder = new ViewHolder();

            if (view == null) {
                view = layoutInflater.inflate(R.layout.list_layout, viewGroup, false);
                viewHolder.tv1 = (TextView)view.findViewById(R.id.textView7);
                viewHolder.tv2 = (TextView)view.findViewById(R.id.textView8);
                viewHolder.img = (ImageView) view.findViewById(R.id.imageView2);

                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }

            viewHolder.tv1.setText(titles[position]);
            viewHolder.tv2.setText(desc[position]);
            viewHolder.img.setImageResource(imageID.getResourceId(position, 0));

            return view;
        }
    }

List_layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        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">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/cplus" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="0dp"
            android:layout_height="32dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:text="TextView"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/imageView2"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView8"
            android:layout_width="0dp"
            android:layout_height="24dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:text="TextView"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/imageView2"
            app:layout_constraintTop_toBottomOf="@+id/textView7"
            tools:text="TextView" />

    </androidx.constraintlayout.widget.ConstraintLayout>

strings.xml:

    <resources>
        <string name="app_name">UIUXAdapterPractical</string>

        <string-array name="Languages">
            <item>Java</item>
            <item>Swift</item>
            <item>C#</item>
            <item>SQL</item>
            <item>Javascript</item>
            <item>Jquery</item>
            <item>C++</item>
        </string-array>

        <string-array name="LanguageDesc">
            <item>Java 的描述</item>
            <item>Swift 的描述</item>
            <item>C# 的描述</item>
            <item>SQL 的描述</item>
            <item>Javascript 的描述</item>
            <item>Jquery 的描述</item>
            <item>C++ 的描述</item>
        </string-array

<details>
<summary>英文:</summary>

The following project was copied verbatim from an online lesson.  It is supposed to display a ListView but nothing shows up.  I am brand new to Android Studio and this is only my second project in it.  There are 5 files and I will display the contents of each of them below.  I just need another pair of eyes or two to find the problem, because I&#39;m &quot;bug blind&quot;.  Any help will be greatly appreciated.

    

&gt; activity_main.xml
&gt;     &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&gt;     &lt;androidx.constraintlayout.widget.ConstraintLayout 
&gt;     xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
&gt;         xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
&gt;         xmlns:tools=&quot;http://schemas.android.com/tools&quot;
&gt;         android:layout_width=&quot;match_parent&quot;
&gt;         android:layout_height=&quot;match_parent&quot;
&gt;         tools:context=&quot;.MainActivity&quot;&gt;
&gt; 
&gt;     &lt;ListView
&gt;         android:id=&quot;@+id/listView&quot;
&gt;         android:layout_width=&quot;0dp&quot;
&gt;         android:layout_height=&quot;0dp&quot;
&gt;         android:layout_marginStart=&quot;16dp&quot;
&gt;         android:layout_marginTop=&quot;16dp&quot;
&gt;         android:layout_marginEnd=&quot;16dp&quot;
&gt;         android:layout_marginBottom=&quot;16dp&quot;
&gt;         android:visibility=&quot;visible&quot;
&gt;         app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
&gt;         app:layout_constraintEnd_toEndOf=&quot;parent&quot;
&gt;         app:layout_constraintStart_toStartOf=&quot;parent&quot;
&gt;         app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt; &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&gt; 
&gt;     MainActivity.java
&gt; 
&gt;     package com.example.tadhg.uiuxadapterpractical;
&gt; 
&gt;     import androidx.appcompat.app.AppCompatActivity;
&gt; 
&gt;     import android.content.Context;
&gt;     import android.content.res.TypedArray;
&gt;     import android.os.Bundle;
&gt;     import android.widget.ListView;
&gt; 
&gt;     public class MainActivity extends AppCompatActivity {
&gt; 
&gt;       ListView lv;
&gt;       Context context;
&gt; 
&gt;       TypedArray images;
&gt;       String [] titleList;
&gt;       String [] descList;
&gt; 
&gt;       @Override
&gt;       protected void onCreate(Bundle savedInstanceState) {
&gt;           super.onCreate(savedInstanceState);
&gt;           setContentView(R.layout.activity_main);
&gt; 
&gt;           context = this;
&gt; 
&gt;           titleList = getResources().getStringArray(R.array.Languages);
&gt;           descList = 
&gt;      getResources().getStringArray(R.array.LanguageDesc);
&gt;           images = 
&gt;      getResources().obtainTypedArray(R.array.LanguageImg);
&gt; 
&gt;           lv = (ListView) findViewById(R.id.listView);
&gt; 
&gt;         }
&gt;     }
&gt; 
&gt; OurAdapter.java
&gt; 
&gt; package com.example.tadhg.uiuxadapterpractical;
&gt; 
&gt; import android.content.Context; import android.content.res.TypedArray;
&gt; import android.view.LayoutInflater; import android.view.View; import
&gt; android.view.ViewGroup; import android.widget.BaseAdapter; import
&gt; android.widget.ImageView; import android.widget.TextView;
&gt; 
&gt; public class OurAdapter extends BaseAdapter {
&gt; 
&gt;     private String [] titles;
&gt;     private String [] desc;
&gt;     private TypedArray imageID;
&gt; 
&gt;     private Context context;
&gt;     private static LayoutInflater layoutInflater;
&gt; 
&gt;     //constructor
&gt;     OurAdapter(Context adapterContext, String[] titleList, String[] descList, TypedArray images){
&gt;         titles = titleList;
&gt;         desc = descList;
&gt;         imageID = images;
&gt;         context = adapterContext;
&gt; 
&gt;         layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
&gt;     }
&gt; 
&gt;     @Override
&gt;     public int getCount() {
&gt;         return titles.length;
&gt;     }
&gt; 
&gt;     @Override
&gt;     public Object getItem(int i) {
&gt;         return i;
&gt;     }
&gt; 
&gt;     @Override
&gt;     public long getItemId(int i) {
&gt;         return i;
&gt;     }
&gt; 
&gt;     private class ViewHolder {
&gt;         TextView tv1, tv2;
&gt;         ImageView img;
&gt;     }
&gt; 
&gt;     @Override
&gt;     public View getView(int position, View view, ViewGroup viewGroup) {
&gt;         ViewHolder viewHolder = new ViewHolder();
&gt; 
&gt;         if (view == null) {
&gt;             view = layoutInflater.inflate(R.layout.list_layout, viewGroup, false);
&gt;             viewHolder.tv1 = (TextView)view.findViewById(R.id.textView7);
&gt;             viewHolder.tv2 = (TextView)view.findViewById(R.id.textView8);
&gt;             viewHolder.img = (ImageView) view.findViewById(R.id.imageView2);
&gt; 
&gt;             view.setTag(viewHolder);
&gt;         } else {
&gt;             viewHolder = (ViewHolder) view.getTag();
&gt;         }
&gt; 
&gt;         viewHolder.tv1.setText(titles[position]);
&gt;         viewHolder.tv2.setText(desc[position]);
&gt;         viewHolder.img.setImageResource(imageID.getResourceId(position, 0));
&gt; 
&gt;         return view;
&gt;     } }
&gt; 
&gt; 
&gt; List_layout.xml
&gt; 
&gt; &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&gt; &lt;androidx.constraintlayout.widget.ConstraintLayout
&gt;     xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
&gt;     xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
&gt;     xmlns:tools=&quot;http://schemas.android.com/tools&quot;
&gt;     android:layout_width=&quot;match_parent&quot;
&gt;     android:layout_height=&quot;match_parent&quot;&gt;
&gt; 
&gt;     &lt;ImageView
&gt;         android:id=&quot;@+id/imageView2&quot;
&gt;         android:layout_width=&quot;wrap_content&quot;
&gt;         android:layout_height=&quot;wrap_content&quot;
&gt;         android:layout_marginStart=&quot;16dp&quot;
&gt;         android:layout_marginTop=&quot;8dp&quot;
&gt;         app:layout_constraintStart_toStartOf=&quot;parent&quot;
&gt;         app:layout_constraintTop_toTopOf=&quot;parent&quot;
&gt;         app:srcCompat=&quot;@drawable/cplus&quot; /&gt;
&gt; 
&gt;     &lt;TextView
&gt;         android:id=&quot;@+id/textView7&quot;
&gt;         android:layout_width=&quot;0dp&quot;
&gt;         android:layout_height=&quot;32dp&quot;
&gt;         android:layout_marginStart=&quot;16dp&quot;
&gt;         android:layout_marginTop=&quot;8dp&quot;
&gt;         android:layout_marginEnd=&quot;8dp&quot;
&gt;         android:text=&quot;TextView&quot;
&gt;         android:textSize=&quot;24sp&quot;
&gt;         app:layout_constraintEnd_toEndOf=&quot;parent&quot;
&gt;         app:layout_constraintStart_toEndOf=&quot;@+id/imageView2&quot;
&gt;         app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&gt; 
&gt;     &lt;TextView
&gt;         android:id=&quot;@+id/textView8&quot;
&gt;         android:layout_width=&quot;0dp&quot;
&gt;         android:layout_height=&quot;24dp&quot;
&gt;         android:layout_marginStart=&quot;16dp&quot;
&gt;         android:layout_marginTop=&quot;8dp&quot;
&gt;         android:layout_marginEnd=&quot;8dp&quot;
&gt;         android:text=&quot;TextView&quot;
&gt;         android:textSize=&quot;18sp&quot;
&gt;         app:layout_constraintEnd_toEndOf=&quot;parent&quot;
&gt;         app:layout_constraintStart_toEndOf=&quot;@+id/imageView2&quot;
&gt;         app:layout_constraintTop_toBottomOf=&quot;@+id/textView7&quot;
&gt;         tools:text=&quot;TextView&quot; /&gt;
&gt; 
&gt; &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&gt; 
&gt; 
&gt; strings.xml
&gt; 
&gt; &lt;resources&gt;
&gt;     &lt;string name=&quot;app_name&quot;&gt;UIUXAdapterPractical&lt;/string&gt;
&gt; 
&gt;     &lt;string-array name=&quot;Languages&quot;&gt;
&gt;         &lt;item&gt;Java&lt;/item&gt;
&gt;         &lt;item&gt;Swift&lt;/item&gt;
&gt;         &lt;item&gt;C#&lt;/item&gt;
&gt;         &lt;item&gt;SQL&lt;/item&gt;
&gt;         &lt;item&gt;Javascript&lt;/item&gt;
&gt;         &lt;item&gt;Jquery&lt;/item&gt;
&gt;         &lt;item&gt;C++&lt;/item&gt;
&gt;     &lt;/string-array&gt;
&gt; 
&gt;     &lt;string-array name=&quot;LanguageDesc&quot;&gt;
&gt;         &lt;item&gt;Desc. of Java&lt;/item&gt;
&gt;         &lt;item&gt;Desc. of Swift&lt;/item&gt;
&gt;         &lt;item&gt;Desc. of C#&lt;/item&gt;
&gt;         &lt;item&gt;Desc. of SQL&lt;/item&gt;
&gt;         &lt;item&gt;Desc. of Javascript&lt;/item&gt;
&gt;         &lt;item&gt;Desc. of Jquery&lt;/item&gt;
&gt;         &lt;item&gt;Desc. of C++&lt;/item&gt;
&gt;     &lt;/string-array&gt;
&gt; 
&gt;     &lt;array name=&quot;LanguageImg&quot;&gt;
&gt;         &lt;item&gt;@drawable/java&lt;/item&gt;
&gt;         &lt;item&gt;@drawable/swift&lt;/item&gt;
&gt;         &lt;item&gt;@drawable/csharp&lt;/item&gt;
&gt;         &lt;item&gt;@drawable/sql&lt;/item&gt;
&gt;         &lt;item&gt;@drawable/js&lt;/item&gt;
&gt;         &lt;item&gt;@drawable/jquery&lt;/item&gt;
&gt;         &lt;item&gt;@drawable/cplus&lt;/item&gt;
&gt;     &lt;/array&gt; &lt;/resources&gt;



</details>


# 答案1
**得分**: 1

你忘记将适配器附加到列表视图...你必须将你的适配器设置到你的`listview`中,然后你才能看到你的列表视图。

**使用以下代码更改你的代码:**

```java
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = this;

    titleList = getResources().getStringArray(R.array.Languages);
    descList = getResources().getStringArray(R.array.LanguageDesc);
    images = getResources().obtainTypedArray(R.array.LanguageImg);
    
    final OurAdapter adapter = new OurAdapter(getApplicationContext(), titleList, descList, images);
    lv = (ListView) findViewById(R.id.listView);
    lv.setAdapter(adapter);
}

希望这能解决你的问题 有人能向我展示为什么ListView没有显示吗?

英文:

you have missed attaching your adapter to listview...you must set your adapter to your listview then only you will see your listview

change your code with following code

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
titleList = getResources().getStringArray(R.array.Languages);
descList =
getResources().getStringArray(R.array.LanguageDesc);
images =
getResources().obtainTypedArray(R.array.LanguageImg);
final OurAdapter adapter = new OurAdapter(getApplicationContext(),titleList,descList,images);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}

hope it will solve your problem 有人能向我展示为什么ListView没有显示吗?

答案2

得分: -1

在你的列表视图中,你有:

android:layout_width="0dp"
android:layout_height="0dp"

改为:

android:layout_width="match_parent"
android:layout_height="match_parent"

你正在强制尺寸为0,所以你看不到它。告诉我是否有效。

英文:

In your listview you have:

android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;0dp&quot;

instead put

android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;

You're forcing the size to be 0 so you can't see it. Tell me if it works

huangapple
  • 本文由 发表于 2020年9月7日 16:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63773929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定