如何在我的arrayAdapter中放置不同的元素?

时间:2016-05-15 23:15:50

标签: android sugarorm

我的问题是:

我有一个带有diferents列的糖数据库,我想在列表视图中输入这些不同的列,我该怎么办?

这是我的代码(FragmentPlanList.java):

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;


import net.elinformaticoenganchado.sergio.workout.entity.Plan;

import java.util.List;

public class FragmentPlanList extends ListFragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.plan_list, container, false);

        if(Plan.count(Plan.class) == 0){

        } else{
            List<Plan> plans = Plan.listAll(Plan.class);
            ArrayAdapter<Plan> adapter = new ArrayAdapter<>(getActivity(),R.layout.list_template, R.id.list_template_name, plans);
            setListAdapter(adapter);
            plans.toArray();
            System.out.println(plans);
        }

        return view;
    }
}

这是我的plan_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        />

</LinearLayout>

这是我的listView行:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/list_template_example"
        android:fontFamily="Arial"
        android:textSize="40sp"
        android:padding="10dp"
        android:id="@+id/list_template_name" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Example"
        android:fontFamily="Arial"
        android:textSize="30sp"
        android:padding="10dp"
        android:id="@+id/list_template_duration"/>
</LinearLayout>

我想将Plans.name(示例)放入带有id list_template_name和Plan.duration的TextView到带有id list_template_duration的TextView中。

感谢您的帮助。

编辑:添加数据库实体:

import com.orm.SugarRecord;
import com.orm.dsl.NotNull;
import com.orm.dsl.Unique;

public class Plan extends SugarRecord{

    @Unique
    protected String name;
    @NotNull
    protected Integer duration;
    @NotNull
    protected String description;

    public Plan() {
    }

    public Plan(String name, Integer duration, String description) {
        this.name = name;
        this.duration = duration;
        this.description = description;
    }

    /**
     * @return String name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return Integer duration
     */
    public Integer getDuration() {
        return duration;
    }

    /**
     * @param duration
     */
    public void setDuration(Integer duration) {
        this.duration = duration;
    }

    /**
     * @return String duration
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "Plan{" +
                "name='" + name + '\'' +
                ", duration=" + duration +
                ", description='" + description + '\'' +
                '}';
    }
}

1 个答案:

答案 0 :(得分:1)

您必须创建自己的Adapter,使用特定类型扩展ArrayAdapter,或者您可以使用Object类型,这样您就可以添加所需的内容。

然后你必须使用instanceof来测试当前元素是什么类型的Object。因此,您将能够应用特定样式,加载特定布局,以呈现此对象。

为例

public class ObjectAdapter extends ArrayAdapter<Object> {
    public ObjectAdapter(Context context, ArrayList<Object> objects) {
        super(context, 0, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position


        try {
            Object object = getItem(position);
            // TODO: Check if an existing view is being reused, otherwise inflate the view
            if (object instance Plan) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.plan_item, parent, false);
                }

                Plan plan = (Plan) object; // cast the object to a Plan

                // populate view with data
                TextView planTitleTV = (TextView) convertView.findViewById(R.id.list_template_name);
                ImageView planImage = (ImageView) convertView.findViewById(R.id. list_template_duration);

                planNameTV.setText(plan.name);
                planDurationTV.setText(plan.duration);

            } else if (object instance PlanB) { // Optional
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.plan_b_item, parent, false);

                PlanB plan = (PlanB) object; // cast the object to a Plan

                // populate view with data
                TextView planTitleTV = (TextView) convertView.findViewById(R.id.plan_b_title);
                ImageView planImage = (ImageView) convertView.findViewById(R.id.plan_b_image);

                planTitleTV.setText(plan.title);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        // Return the completed view to render on screen
        return convertView;
    }
}