【问题标题】:Bind ArrayList<ArrayList<>> in Custom Adapter in Android在 Android 的自定义适配器中绑定 ArrayList<ArrayList<>>
【发布时间】:2017-07-21 20:40:58
【问题描述】:

我想制作一个包含多个String 和一个ArrayList&lt;ArrayList&lt;GeofenceDetails&gt;&gt; 的自定义适配器。

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

但问题是适配器不显示任何数据。我认为我的自定义适配器出了点问题,但我无法弄清楚。

感谢您的建议。告诉我如何创建自定义适配器来显示多个字符串和ArrayList&lt;ArrayList&lt;&gt;&gt;

提前致谢。

【问题讨论】:

  • 关于代码质量的两件事:A) 命名......在你的名字中输入确切的类型是没有意义的 B) 你也确实想要使用特定的您的类型的实现类;你最好直接写private List&lt;Geofences&gt; geofences = new ArrayList&lt;&gt;();。更容易阅读和使用!
  • 没有数据显示,因为您在适配器中发送了错误的数组列表。您使用 ArrayList 定义了 GeofenceNameListAdapter 构造函数,而使用 ArrayList>() 初始化适配器。
  • 感谢@GhostCat。我会这样做的。
  • 我没有看到 setAdapter 方法调用。你有吗?
  • @Alvi 我没听懂你。我正在我的适配器中发送 geofencesArrayList ,这是确切的。

标签: android arraylist android-arrayadapter


【解决方案1】:

您正在使用ArrayAdapter 构造函数,该构造函数将第三个参数作为视图资源 ID,但您将数组大小放在那里。

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

首先,更改构造函数调用(放置第三个参数,例如 R.id.list_item 或只使用 2 参数构造函数)。

那么你应该在你的GeofenceNameListAdapter 类中重写getCount 方法。

@Override
public int getCount() {
    return geofences.size();
}

【讨论】:

    猜你喜欢
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    相关资源
    最近更新 更多