2017-03-02 62 views
1

我想打一個自定義的adaper其中將包含多個StringArrayList<ArrayList<GeofenceDetails>>綁定的ArrayList <ArrayList<>>在自定義適配器在Android中

private ArrayList<Geofences> geofencesArrayList; 
geofencesArrayList = new ArrayList<Geofences>(); 
assetNameArrayList = new ArrayList<ArrayList<GeofenceDetails>>(); 

geofencesArrayList.add(new Geofences(id,name,type,assetNameArrayList)); 
adapter = new GeofenceNameListAdapter(getActivity(),geofencesArrayList); 

adapter.notifyDataSetChanged(); 
geoListView.setAdapter(adapter); 

這裏是我的自定義適配器

public class GeofenceNameListAdapter extends ArrayAdapter<ArrayList<Geofences>> { 
    private final Activity context; 
    private final ArrayList<Geofences> geofences; 
    //song list and layout 
    private LayoutInflater view; 
// private static LayoutInflater inflater=null; 

    public static final String PREFERENCES = "RPREFS"; 
    SharedPreferences sharedprefs = null; 
    private String ord_id = null; 
    private String email = null; 

    public GeofenceNameListAdapter(Activity context, 
            ArrayList<Geofences> geofences) { 

     super(context, R.layout.layout_geofence, geofences.size()); 
     // TODO Auto-generated constructor stub 

     finder_sharedprefs = getContext().getSharedPreferences(PREFERENCES, getContext().MODE_PRIVATE); 

     finder_ord_id = finder_sharedprefs.getString("org_id", null); 
     finder_email = finder_sharedprefs.getString("email", null); 

     this.context = context; 
     this.geofences = geofences; 
    } 
    public View getView(final int position, View view, ViewGroup parent) { 

     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView = inflater.inflate(R.layout.layout_geofence, null, true); 

     TextView txtTitle = (TextView) rowView.findViewById(R.id.name); 
     TextView txtType = (TextView) rowView.findViewById(R.id.type);rowView.findViewById(R.id.delete); 

    txtTitle.setText(geofences.get(position).getTitle()); 
    txtType.setText(geofences.get(position).getType()); 

    return rowView; 
} 

我可以看到geofencesArrayList一切。這裏我打印的數據是geofencesArrayList

for (int l = 0; l<geofencesArrayList.size(); l++) { 
     Log.d("GEOFENCE_DETAILS", geofencesArrayList.get(l).getId().toString()); 
     Log.d("GEOFENCE_DETAILS", geofencesArrayList.get(l).getTitle().toString()); 
     Log.d("GEOFENCE_DETAILS", geofencesArrayList.get(l).getType().toString()); 
    // Log.d("GEOFENCE_DETAILS", geofencesArrayList.get(l).getPoints().toString()); 
     Log.d("GEOFENCE_DETAILS", geofencesArrayList.get(l).getEnforcedData().toString()); 
} 

但問題是Adapter不顯示任何數據。我認爲我的自定義適配器出了問題,但我無法弄清楚。

我感謝您的建議。讓我知道如何創建一個自定義適配器來顯示多個字符串和ArrayList<ArrayList<>>

在此先感謝。

+1

關於代碼質量的兩件事:A)命名......將確切類型放入您的名稱沒有意義B)您也可以**不**要在您的類型上使用特定的實現類;你最好直接寫'private List geofences = new ArrayList <>();'。更容易閱讀和使用! – GhostCat

+0

沒有數據顯示,因爲您在適配器中發送了錯誤的數組列表。您定義的GeofenceNameListAdapter構造與ArrayList的而與初始化ArrayList中的適配器>()。 – Alvi

+0

謝謝@GhostCat。我去做。 – anuradha

回答

0

您正在使用ArrayAdapter構造函數得到3擋的參數作爲視圖資源ID,但你是把有數組的大小。

super(context, R.layout.layout_geofence, geofences.size()); 

首先,改變構造函數的調用(代替3擋的參數類似R.id.list_item或只使用2個參數的構造函數)。

那麼你應該重寫你的GeofenceNameListAdaptergetCount方法。

@Override 
public int getCount() { 
    return geofences.size(); 
} 
相關問題