ArrayList的内容<hashmap <string,string>&gt;在android </hashmap <string,string>中膨胀两个不同的布局时,没有在ListView中设置

时间:2014-09-12 07:09:54

标签: android listview layout-inflater simpleadapter

我正在尝试inflate使用Layouts ListView SimpleAdapter ArrayList<HashMap<String,String>>。我做了以下编码,两个布局都被夸大了,但 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); 的内容没有设置到ListView中。任何人都可以一步一步地指导我出错的地方吗?

{{1}}

2 个答案:

答案 0 :(得分: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;
    }

}

希望它会有所帮助。 :)

答案 1 :(得分:0)

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