android - Android 中的 ListView 是如何工作的?我很困惑

标签 android android-listview android-relativelayout android-arrayadapter

我正在尝试在 android 中使用一个简单的 ListView 。 我从某个网站得到了一个例子。有效。代码如下:

主布局文件:(res/layout/main.xml)

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      android:orientation="vertical"  
      android:layout_width="fill_parent"  
      android:layout_height="fill_parent">  

    <ListView android:layout_width="fill_parent"   
      android:layout_height="fill_parent"   
      android:id="@+id/mainListView">  
    </ListView>  

</LinearLayout>  

行文件:(res/layout/simplerow.xml)

<TextView xmlns:android="http://schemas.android.com/apk/res/android"  
 android:id="@+id/rowTextView"   
 android:layout_width="fill_parent"   
 android:layout_height="wrap_content"  
 android:padding="10dp"  
 android:textSize="16sp" >  
</TextView> 

主要 Activity :

package com.windrealm.android;  

import java.util.ArrayList;  
import java.util.Arrays;  

import android.app.Activity;  
import android.os.Bundle;  
import android.widget.ArrayAdapter;  
import android.widget.ListView;  

public class SimpleListViewActivity extends Activity {  

  private ListView mainListView ;  
  private ArrayAdapter<String> listAdapter ;  

  /** Called when the activity is first created. */  
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  

  // Find the ListView resource.   
  mainListView = (ListView) findViewById( R.id.mainListView );  

  // Create and populate a List of planet names.  
  String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",  
                                  "Jupiter", "Saturn", "Uranus", "Neptune"};    
  ArrayList<String> planetList = new ArrayList<String>();  
  planetList.addAll( Arrays.asList(planets) );  

  // Create ArrayAdapter using the planet list.  
  listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);  

  // Add more planets. If you passed a String[] instead of a List<String>   
  // into the ArrayAdapter constructor, you must not add more items.   
  // Otherwise an exception will occur.  
  listAdapter.add( "Ceres" );  
  listAdapter.add( "Pluto" );  
  listAdapter.add( "Haumea" );  
  listAdapter.add( "Makemake" );  
  listAdapter.add( "Eris" );  

  // Set the ArrayAdapter as the ListView's adapter.  
  mainListView.setAdapter( listAdapter );        
  }  
}

我试图添加一张图片,甚至不是动态的。我只是想在 simplerow.xml 文件中添加一个布局。但它不起作用。

代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/songs"
        android:src="@drawable/song_bg" />

    <ImageView
        android:id="@+id/albumArt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:contentDescription="@string/album"
        android:src="@drawable/albumart_shanghai" />

    <TextView
        android:id="@+id/rowTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/albumArt"
        android:layout_toRightOf="@+id/albumArt"
        android:layout_marginLeft="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/black" />

    <ImageButton
        android:id="@+id/imageView3"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@null"
        android:contentDescription="@string/arrow"
        android:src="@drawable/arrow_icon" />

</RelativeLayout>

我们不能在 ListView 中添加 Relative 布局? 请帮助我一个月以来我对 ListView 感到震惊。我什至不能移动一步:( 我是 Java 和 XML 的新手。请有人告诉我正确的路径。

最佳答案

首先你需要扩展ListActivity,比如

public class MyListActivity extends ListActivity{}

那么您可以简单地将适配器指定为

setListAdapter(adapter);

你的布局文件有问题,android ListView id 应该是 @android:id/list,android 开发者网站说

your own view MUST contain a ListView object with the id "@android:id/list"

编辑

基本上,您使用的数组适配器构造函数签名是,

ArrayAdapter(Context context, int textViewResourceId, List<T> objects)

但是如果你想使用更复杂的布局,使用

public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects)

textViewResourceId 应该指布局中的 textview id。

这显然会导致,您可以拥有包含多个文本字段的布局,但给定的文本将显示在由 id 指定的文本字段中。

因此,为了完成您的任务,您需要创建自己的适配器来扩展 BaseAdapter。在该适配器中,您需要覆盖

public View getView(int position, View convertView, ViewGroup parent) {}

方法,您可以在其中扩充布局并设置适当的值。

我想你至少已经有了一些想法,然后看看 this关于 ListView 的最详细的教程。

关于android - Android 中的 ListView 是如何工作的?我很困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11791134/

相关文章:

android - 多个dex文件定义Lcom/google/zxing/BarcodeFormat

android - 动态ListView几个item响应点击

java - Android 应用程序 ListView 启动太慢

android - 将图像添加到 Android ListView 的简单方法

android - 在android listView中过滤搜索

android - 为什么标题不在工具栏居中?

android - 以编程方式将属性 AlignParentRight 设置为 RelativeLayout 中的按钮不起作用?

android - Android驱动开发的一些问题

android - 在 listView 自定义适配器中获取父 viewHolder

android - TextView 中文本顶部的额外空间?