Android自定义 ListView 行

标签 android listview row

我正在努力为我的 ListView 行分配多个 TextView 。 我希望我的行有一个用于人名的 TextView ,一个用于地址,一个用于年龄 - 但我没有成功这样做。

如果有人能给我提供一个简单的例子,那就太好了。

谢谢!

这是我自定义的 ArrayAdapter

    import java.util.ArrayList;

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

public class MyAdapter extends BaseAdapter {
    private LayoutInflater mInflater = null;
    private ArrayList<Invoice> peopleList;

    private final class ViewHolder {
        TextView kidLabel;
        TextView restLabel;
        TextView fristLabel;
        TextView amountLabel;
    }

    private ViewHolder mHolder = null;

    public MyAdapter(Context context) {
        Context mContext = context;
        mInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            mHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.row, null);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        mHolder.kidLabel = (TextView) convertView.findViewById(R.id.kidLabel);
        mHolder.kidLabel.setText(peopleList.get(position).getKID());

        mHolder.fristLabel = (TextView) convertView
                .findViewById(R.id.fristLabel);
        mHolder.fristLabel.setText(peopleList.get(position).getDueDate());

        mHolder.restLabel = (TextView) convertView.findViewById(R.id.restLabel);
        mHolder.restLabel.setText(peopleList.get(position).getDueAmount());

        mHolder.amountLabel = (TextView) convertView
                .findViewById(R.id.amountLabel);
        mHolder.amountLabel.setText(peopleList.get(position).getDueAmount());

        return convertView;
    }
}

这是我的自定义行 xml:

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="45dip"
        android:src="@drawable/cellback" android:scaleType="fitXY"/>

    <TextView
        android:id="@+id/kidLabel"
        android:layout_width="160dip"
        android:layout_height="25dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="13dip" android:textColor="#333"/>

    <TextView
        android:id="@+id/fristLabel"
        android:layout_width="160dip"
        android:layout_height="20dip"
        android:layout_marginTop="25dip"
        android:textSize="11dip" android:textColor="#999"/>

    <TextView
        android:id="@+id/amountLabel"
        android:layout_width="160dip"
        android:layout_height="25dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="13dip" android:layout_marginLeft="160dip" android:gravity="right" android:textColor="#333"/>

    <TextView
        android:id="@+id/restLabel"
        android:layout_width="160dip"
        android:layout_height="20dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="11dip" android:layout_marginLeft="160dip" android:layout_marginTop="25dip" android:gravity="right" android:textColor="#999"/>

</RelativeLayout>

最佳答案

首先,你应该创建一个xml来描述你的列表单元格喜欢什么,叫做cell.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="wrap_content"
    android:orientation="vertical" android:background="@color/spink">

    <TextView
        android:id="@+id/name_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/address_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Address"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

其次,创建一个适配器。它可以帮助您的 ListView 显示数据:

public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater = null;
private ArrayList<People> peopleList;

private final class ViewHolder {
TextView nameTextView;
TextView addressTextView;
}

private ViewHolder mHolder = null;

public MyAdapter(Context context) {
mContext = context;
mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
mHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.cell, null);           
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder)convertView.getTag(); 
}

mHolder.nameTextView (TextView)convertView.findViewById(R.id.name_textView);
mHolder.nameTextView.setText(peopleList.get(position).getName());
mHolder.addressTextView = (TextView)convertView.findViewById(R.id.address_textView);
mHolder.addressTextView.setText(peopleList.get(position).getAddress());

return convertView;
}
}

最后,当您想要显示数据时,在您的 Activity 中执行此操作:

listView.setAdapter(new MyAdapter());

希望对你有帮助。

关于Android自定义 ListView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10346348/

相关文章:

listview - jquery移动 ListView : how to dynamically set themes for individual <li>'s

c# - 完全隐藏 GridView 中的元素(用作 ListView 的 View )?

css - 我可以将内容放在 bootstrap 3 网格的一行中吗?

Android: float 操作按钮内的细节动画

css - JQuery 手机 : Programmatically inserted Button's style doesn't apply

java - 尝试将数据从 Activity 传递到 fragment 时始终获得空值

R:选择高于某个阈值的 n 个连续行中的第一个

c# - GridView 在 C# 中添加第二个和第三个标题行

android - 在 tabhost 中的 fragment 之间传递数据

android - 使用 Kivy 访问电话簿(联系人)