【问题标题】:How does ListView in Android works ? I'm confusedAndroid 中的 ListView 是如何工作的?我很困惑
【发布时间】:2012-08-04 13:11:05
【问题描述】:

我在 android 中尝试了一个简单的列表视图。 我从某个网站上得到了一个例子。有效。代码如下:

主布局文件:(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> 

主要活动:

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 中添加相对布局? 请帮助我从一个月开始就对 ListView 感到震惊。我什至不能移动一步:( 我是 java 和 XML 的新手。请有人告诉我正确的道路。

【问题讨论】:

  • 我们不能在 ListView 中添加相对布局? ——那么你面临的具体问题是什么?发生了什么?
  • 尝试阅读this..也许会澄清
  • @AndroSelva,那么它实际上是如何工作的?我需要为每一行实现一个设计。就像每行的图像和其中的 2 个文本视图。我怎样才能实现它?
  • @praveenkumar 您面临的具体问题是什么?你有例外吗?你没在屏幕上看到什么吗?列表行显示不正确?等
  • @Luksprog ya.. 我获得豁免并要求我关闭。什么都没有出现..我在这里遇到错误mainListView.setAdapter( listAdapter );

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


【解决方案1】:

首先你需要扩展ListActivity,比如

public class MyListActivity extends ListActivity{}

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

setListAdapter(adapter);

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

您自己的视图必须包含一个带有 id 的 ListView 对象 "@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关于列表视图的解释最多的教程。

【讨论】:

    【解决方案2】:

    Vogella 给出了完美的理解。请通过它。它甚至为您提供一步一步的信息。

    【讨论】:

      【解决方案3】:

      列表视图示例

      布局文件:

          <?xml version="1.0" encoding="utf-8"?>
          <TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="10dp"
          android:textSize="20sp" >
          </TextView>
      

      主要活动:

      public class MainActivity extends ListActivity {
      
      static final String[] NUM= new String[] { "1", "2", "3",
              "4", "5", "6", "7", "8",
              "9", "10", "11", "12", "13" };
      
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
      
      
          setListAdapter(new ArrayAdapter<String>(this, R.layout.list_num,NUM));
      
          ListView listView = getListView();
          listView.setTextFilterEnabled(true);
      
          listView.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View view,
                      int position, long id) {
                  // When clicked, show a toast with the TextView text
                  Toast.makeText(getApplicationContext(),
                  ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
              }
          });
      
      }
      
      }
      

      【讨论】:

      • 我已经这样做并且工作正常,我在这里发布了问题因为我不知道如何在 listView 行中实现相对布局,现在我已经实现了 Realtive 布局我不知道如何在行中添加两个 textView 并向其发送值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2012-08-04
      相关资源
      最近更新 更多