【问题标题】:Contents of ArrayList<HashMap<String,String>> not getting set in ListView when inflating two different layouts in androidArrayList<HashMap<String,String>> 的内容在 android 中膨胀两个不同的布局时未在 ListView 中设置
【发布时间】:2014-09-12 07:09:54
【问题描述】:

我正在尝试使用SimpleAdapter 在我的ListViewinflate 两个不同的Layouts。我已经完成了以下编码,两个布局都在膨胀,但是 ArrayList&lt;HashMap&lt;String,String&gt;&gt; 的内容没有设置到 ListView 中。谁能一步一步指导我哪里出错了?

 ListAdapter k = new SimpleAdapter(Table_Order_Activity.this,
            cart_activity.table_order_items, R.layout.menulist,
            new String[] { "Food_Name", "Pref_Name", "Food_Currency",
                    "Food_Price", "Food_Price_Total", "Pref_ID",
                    "Food_Image" }, new int[] { R.id.cat_name,
                    R.id.textView1, R.id.textView2, R.id.textView3,
                    R.id.url, R.id.veggie, R.id.Category }) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View v = super.getView(position, convertView, parent);

            LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                    .getSystemService(
                            Table_Order_Activity.this.LAYOUT_INFLATER_SERVICE);
            int type = getItemViewType(position);
            Log.i("position + convertview + type ", "" + position + ","
                    + convertView + "," + type);
            TextView image_url = (TextView) v.findViewById(R.id.Category);
            ImageView image = (ImageView) v.findViewById(R.id.imageView1);
            imageloader.DisplayImage(image_url.getText().toString(), image);

            if (type == 0) {

                v = inflater.inflate(R.layout.r2, parent, false);
            } else if (type == 1) {
                v = inflater.inflate(R.layout.menulist, parent, false);

            }
            return v;
        }

        @Override
        public int getItemViewType(int position) {
            int type = 0;
            Log.i("position in getitemviewtype", "" + position);
            if (position == 0) {
                type = FIRST_TYPE;
            } else if (position == 1) {
                type = SECOND_TYPE;
            }
            /*
             * else if (position == 2) { type = FIRST_TYPE; } else if
             * (position == 3) { type = SECOND_TYPE; }
             */
            return type;
        }

    };

    table_order_list.setAdapter(k);

【问题讨论】:

  • 为什么不扩展 BaseAdapter 或 ArrayAdapter?

标签: android listview layout-inflater simpleadapter


【解决方案1】:

您已经覆盖了简单适配器的 get view 方法并且它搞砸了 v 被另一个没有数据绑定的布局覆盖。不要使用简单的适配器,而是通过扩展基本适配器来创建自己的自定义适配器。

public class CustomAdapter extends BaseAdapter {

    public static final int FIRST_TYPE = 0;
    public static final int SECOND_TYPE = 1;

    ArrayList<HashMap<String, String>> data;
    Context mContext;

    public CustomAdapter(Context context,
            ArrayList<HashMap<String, String>> data) {
        this.data = data;
        this.mContext = context;
    }

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

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        int type = FIRST_TYPE;
        if (position == 0) {
            type = FIRST_TYPE;
        } else if (position == 1) {
            type = SECOND_TYPE;
        }

        return type;

    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int type = getItemViewType(position);       

        if (type == FIRST_TYPE) {
            v = inflater.inflate(R.layout.r2, parent, false);
        } else if (type == SECOND_TYPE) {
            v = inflater.inflate(R.layout.menulist, parent, false);
        }

        TextView image_url = (TextView) v.findViewById(R.id.Category);
        ImageView image = (ImageView) v.findViewById(R.id.imageView1);
        imageloader.DisplayImage(image_url.getText().toString(), image);

        //Set your data here to views.
        return v;
    }

}

希望它会有所帮助。 :)

【讨论】:

    【解决方案2】:

    在尝试帮助您之前:
    为什么不扩展 BaseAdapter 或 ArrayAdapter?
    在以下链接中,您可以找到有关如何实现所需内容的详细信息:
    http://www.vogella.com/tutorials/AndroidListView/article.html.
    http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/
    你的情况很清楚,自定义适配器。如果这家伙懒得解释到这种程度,我会为你做的,但它就在那里......

    【讨论】:

      猜你喜欢
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 2011-04-15
      • 2013-05-12
      • 2013-03-22
      相关资源
      最近更新 更多