ListView中显示对象的详细信息 [英] ListView displaying object's details

查看:116
本文介绍了ListView中显示对象的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Andr​​oid应用程序实现的是有一个的ListView 这是从对象的显示领域。可以说我有叫人与像姓名,地址,年龄等属性的对象。我想有从每个人的财产在的ListView一个项目(名称将是第一排,地址将是第二排...)。

What I am trying to achieve in my Android app is to have a ListView which is displaying fields from objects. Lets say I have an object called Person with properties like Name, Address, Age and so on. I want to have each property from Person as one item in a ListView (Name would be in first row, address would be in second row...).

我怎么能这样做,如果我不使用数组作为的ListView 的来源,而且从对象的属性?我到处都可以找到ArrayList和ArrayAdapter教程,但这个我不能使用。谢谢。

How can I do that if I am not using array as a source of ListView but properties from object? Everywhere I can find tutorials with ArrayList and ArrayAdapter but this I can't use. Thanks.

推荐答案

有关这一项,你需要创建一个适配器和一个单一的视图来加载列表,

For this one you need to create a adapter and a single view to load for list,

例如,
创建布局资源文件这样,

for example, create a layout resource file like this,

//布局/ single_rsr_list_item.xml

//layout/single_rsr_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/list_item_back">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:id="@+id/img_sin_list_image"
                android:background="@color/black"
                android:layout_marginTop="35dp"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.2">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="SomeText SomeText SomeText SomeTextandroid:elliklm;ml;;m;psize="
                    android:id="@+id/tv_single_list_rsr_description"
                    android:textColor="@color/black"
                    android:textSize="15sp"
                    android:layout_marginTop="20dp"
                    android:layout_marginRight="45dp"
                    android:maxEms="11"
                    android:lines="2"
                    android:ellipsize="end"
                    android:layout_marginLeft="20dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="2015/03/13 1:10"
                    android:id="@+id/tv_sin_list_rsr_date"
                    android:layout_below="@+id/tv_single_list_rsr_description"
                    android:layout_marginTop="5dp"
                    android:textSize="12sp"
                    android:layout_marginLeft="20dp"
                    android:textColor="@color/ring_color" />

                <TextView
                    android:layout_width="wrap_content"
                    android:visibility="gone"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Small Text"
                    android:id="@+id/tv_sin_list_rsr_id"
                    android:layout_below="@+id/tv_sin_list_rsr_date"
                    android:layout_alignLeft="@+id/tv_sin_list_rsr_date"
                    android:layout_alignStart="@+id/tv_sin_list_rsr_date" />
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

现在创建一个克拉斯得到你所需要的就是这样,

now create a claas to get what you need like this,

public class RSRData {
    private int id;
    private String description;
    private String date;
    private String imagePath;

    public RSRData() {
    }

    public RSRData(int id, String imagePath, String description, String date) {
        this.id = id;
        this.imagePath = imagePath;
        this.description = description;
        this.date = date;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getImagePath() {
        return imagePath;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }
}

然后WITE这样的适配器与此逆定理到重复列表这样的,

then wite a adapter like this with converst this to a repeating list like this,

public class RSRAdapter extends ArrayAdapter<RSRData> {

    Context con;
    int res;

    public RSRAdapter(Context context,int resource,ArrayList<RSRData> rsrData){
        super(context,resource,rsrData);
        this.con = context;
        this.res = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RSRData rsrData=getItem(position);

        if(convertView==null){
            LayoutInflater inflator=LayoutInflater.from(con);
            convertView=inflator.inflate(res,parent,false);
        }

        TextView tvTitle = (TextView)convertView.findViewById(R.id.tv_single_list_rsr_description);
        tvTitle.setText(rsrData.getDescription());

        TextView tvId = (TextView)convertView.findViewById(R.id.tv_sin_list_rsr_id);
        tvId.setText(rsrData.getId());

        TextView date = (TextView)convertView.findViewById(R.id.tv_sin_list_rsr_date);
        date.setText(rsrData.getDate());

      /*  ImageVi = (TextView)convertView.findViewById(R.id.tv_primaryKey);
        tvPrimaryKey.setText(String.valueOf(promotionData.getId()));*/


        return convertView;
    }
}

全部完成后,使用该数据设置为您的列表视图,

after all done, use this to set data to your list view,

ListView list = (ListView)findViewById(R.id.your_list);
ArrayList<RSRAdapter> data = new ArrayList<>(); // use this to get data from database and pass to here,

RSRAdapter adapter = new RSRAdapter(this,R.layout.sigle_promotion_item,data);
        list.setAdapter(adapter);

这篇关于ListView中显示对象的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆