2016-05-15 85 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