2016-05-15 105 views
0

我的问题是:我怎样才能把differents元素放在我的arrayAdapter中?

我有一个糖数据库与diferents列,我想列入我的列表查看这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_durati的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

你必须创建自己的适配器,ArrayAdapter与特定类型的扩展,或者您可以使用Object类型,所以你可以添加你什么都想要。

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

为例

​​
+0

太谢谢你了:) – Sermanes

+0

注:您可以在ArrayAdapter ,如果你在你的名单只有计划。阻止必须施放。 – sonique

+0

还有一件事我试图称这个类,但不工作 列表 plans = Plan.listAll(Plan.class); PlanAdapter adapter = new PlanAdapter(getActivity(),R.layout.list_template,plans); 我有像你一样的代码,但唯一的变化是,我有一个不是arrayList的列表 – Sermanes

相关问题