ListView与自定义行布局 - Android

时间:2012-07-01 12:30:10

标签: java android android-listview android-arrayadapter

我想创建一个包含列表的Activity,其中行具有自定义布局。 所以我创建了list_entry_layout.xml文件,定义了列表中每一行应该具有的布局(在我的示例中,每行应该有一个标题和摘要):

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/list_entry_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp" >
    </TextView>

    <TextView
        android:id="@+id/list_entry_summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp" >
    </TextView>

</LinearLayout>

我的问题是我不知道如何将数据添加到ListActivity类的每一行。 使用以下代码片段,我可以添加每行的标题:

public class MyActivity extends ListActivity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    { 

        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);

        ListView listView = (ListView) findViewById(android.R.id.list);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };

        ArrayAdapter<String> titleAdapter = new ArrayAdapter<String>(this, R.layout.list_entry_layout, R.id.list_entry_title, values);
        // Assign adapter to ListView
        listView.setAdapter(titleAdapter);

    }
}
  

还要添加摘要我该怎么做?

如果我添加此代码,我会将摘要可视化,而不是标题:

String[] values = new String[] { "Android_summary", "iPhone_summary", "WindowsMobile_summary", "Blackberry_summary", "WebOS_summary", "Ubuntu_summary", "Windows7_summary", "Max OS X_summary", "Linux_summary", "OS/2_summary" };
ArrayAdapter<String> summaryAdapter = new ArrayAdapter<String>(this, R.layout.list_entry_layout, R.id.list_entry_summary, values);
// Assign adapter to ListView
listView.setAdapter(summaryAdapter);

以下是我想获得的结果:

enter image description here

3 个答案:

答案 0 :(得分:32)

您需要创建自己的 ArrayAdapter

private class YourAdapter extends ArrayAdapter<String> {
   // do some work
}

然后你应该指定如何使用XML查看你的行,完全符合你的目标,我建议你使用RelativeLayout,它看起来像这样:

<强> row.xml

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

    <TextView 
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        />

    <TextView 
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/name"
        />

</RelativeLayout>

那么在 YourAdapter 中你必须设置超级构造器:

public YourAdapter() {
   super(YourActivity.this, R.layout.row, data);
}

然后,为了在ListView +更有效的实施中自定义您的数据,我建议您覆盖getView()方法并使用Holder design pattern

@Override
public View getView(int position, View convertView, ViewGroup parent) {         
   ViewHolder holder = null;
   LayoutInflater inflater = getLayoutInflater();
      if (convertView == null) {
         convertView = inflater.inflate(R.layout.row, null, false);
         holder = new ViewHolder(convertView);
         convertView.setTag(holder);
      }
      else {
         holder = (ViewHolder) convertView.getTag();
      }     
      holder.getUpperText().setText(dataSource[position]);
      holder.getLowerText().setText(dataSource[position]);

   return convertView;  
}

最后只需初始化ListView并设置Adapter

ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(new YourAdapter());


注意: Design pattern Holder表示包含每行子窗口小部件的任意对象,因此您只需找到一次,然后使用Holder对象将始终可以访问它们。

Holder的实现可能如下所示:

public class ViewHolder {
   private View row;
   private TextView upperText = null, lowerText = null;

   public ViewHolder(View row) {
      this.row = row;
   }

   public TextView getUpperText() {
      if (this.upperText == null) {
         this.upperText = (TextView) inView.findViewById(R.id.someId);
      }
      return this.upperText;
   }

   public TextView getLowerText() {
      if (this.lowerText == null) {
         this.lowerText = (TextView) inView.findViewById(R.id.someId);
      }
      return this.lowerText;
   }
}
希望它有所帮助。

答案 1 :(得分:13)

您可以使用android.R.layout.simple_list_item_2实现此布局,而不是创建自定义行布局。

无论如何,如果您想使用自定义行布局方法,那么我已经为您准备了代码段。

你走了。

<强> SampleActivity.java

package org.sample;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.TwoLineListItem;

public class SampleActivity extends ListActivity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Mobile mobile;

        ArrayList<Mobile> mobiles  new ArrayList<Mobile>();

        mobile = new Mobile();
        mobile.setName("Android");
        mobile.setSummary("summary goes here");
        mobiles.add(mobile);

        mobile = new Mobile();
        mobile.setName("Blackberry");
        mobile.setSummary("summary goes here");
        mobiles.add(mobile);

        setListAdapter(new MyAdapter(this, mobiles));
    }

}

<强> Mobile.java

class Mobile {
    String name;
    String summary;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

}

<强> MyAdapter.java

class MyAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Mobile> mobiles;

    public MyAdapter(Context context, ArrayList<Mobile> mobiles) {
        this.context = context;
        this.mobiles = mobiles;
    }

    @Override
    public int getCount() {
        return mobiles.size();
    }

    @Override
    public Object getItem(int position) {
        return mobiles.get(position);
    }

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

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


        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = (View) inflater.inflate(
                    R.layout.list_entry_layout, null);
        }

        TextView name = (TextView)convertView.findViewById(R.id.list_entry_title);
        TextView summary=(TextView)convertView.findViewById(R.id.list_entry_summary);

        name.setText(mobiles.get(position).getName());
        summary.setText(mobiles.get(position).getSummary());

        return convertView;
    }
}

答案 2 :(得分:4)

建立自己的ArrayAdapter 请参阅示例http://www.ezzylearning.com/tutorial.aspx?tid=1763429