如何将不同的元素放入我的 arrayAdapter 中?

How can I put differents elements in my arrayAdapter?

我的问题是:

我有一个包含不同列的糖数据库,我想将这些不同列放入我的列表视图中,我该怎么做?

这是我的代码(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 的 TextView 中,将 Plan.duration 放入 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 + '\'' +
                '}';
    }
}

您必须创建自己的适配器,使用特定类型扩展 ArrayAdapter,或者您可以使用 Object 类型,这样您就可以添加任何您想要的。

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

例子

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;
    }
}