zoukankan      html  css  js  c++  java
  • 大叔的第11天 @ListView适配器

    还不太明白,代码放这里,弄懂之后应该是个跨越

    这篇文章可以辅助理解http://www.cnblogs.com/topcoderliu/archive/2011/05/07/2039862.html

    public class PackageOneActivity extends NetWorkBaseActivity {
        private ListView oneListView;
        private ArrayList<PackageListItem> mList;
        private PackageListAdapter mAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.package_one);
    
            oneListView = (ListView) findViewById(R.id.package_detail_listview);
            oneListView.setDivider(null);
            mList = new ArrayList<PackageListItem>();
            HashMap<String, String> map1 = (HashMap<String, String>) getIntent().getSerializableExtra("Data");
            String caseId = map1.get("packageid");
            int id = Integer.parseInt(map1.get("packageid"));
            switch (id) {
            case 1:
                titleText.setText("小保养");// /自定义布局赋值 ---4
                break;
            case 2:
                titleText.setText("大保养");// /自定义布局赋值 ---4
                break;
            }
    
            mAdapter = new PackageListAdapter(PackageOneActivity.this, mList);
            oneListView.setAdapter(mAdapter);
            Log.v("caseid", map1.get("packageid"));
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            BasicNameValuePair pair4 = new BasicNameValuePair("StyleId", "7780");// 附加信息
            params.add(pair4);
            BasicNameValuePair pair3 = new BasicNameValuePair("CaseId", "1");// 附加信息
            params.add(pair3);
            BasicNameValuePair pair1 = new BasicNameValuePair("UserAgent", Constands.UserAgent);// 附加信息
            params.add(pair1);
            BasicNameValuePair pair2 = new BasicNameValuePair("Token", "34430000003165727730");// 附加信息
            params.add(pair2);
    
            new HttpConnection().post("maintenanceshop", params, new HttpConnection.CallbackListener() {
                public void callBack(String result) {
                    Log.v("post了", result);
                    if (result != "fail") {
                        try {
                            // JASON解析
                            JSONObject object = new JSONObject(result);
                            String json_result = object.getString("Result");
                            JSONArray content = object.getJSONArray("Content");
                            switch (Integer.valueOf(json_result)) {
                            case 0:
                                Log.v("json_result", "0");
                                int cc = content.length();
                                for (int i = 0; i < cc; i++) {
                                    PackageListItem item = new PackageListItem(content.getJSONObject(i));
                                    mList.add(item);
                                }
                                mAdapter.notifyDataSetChanged();
                                break;
                            default:
                                break;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                    }
                }
    
            });
        }
    
        public class PackageListAdapter extends BaseAdapter {
            private ArrayList<PackageListItem> packageList;
            private LayoutInflater inflater;
    
            private OnClickListener mListerner = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                    case R.id.list_complany_name:
                        v.getTag(); // 取得v的Tag
                        Log.v("button", "name");
                        break;
                    case R.id.list_complany_address:
                        Log.v("button", "list_complany_address");
                        break;
                    case R.id.list_complany_phone:
                        Log.v("button", "list_complany_phone");
                        break;
                    case R.id.list_complany_detail:
                        Log.v("button", "list_complany_detail");
                        break;
                    default:
                        break;
                    }
    
                }
            };
    
            public PackageListAdapter(Context context) {
                inflater = LayoutInflater.from(context);
            }
    
            public PackageListAdapter(Context context, ArrayList<PackageListItem> urgents) {
                this.packageList = urgents;
                inflater = LayoutInflater.from(context);
                notifyDataSetInvalidated();
            }
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return packageList == null ? 0 : packageList.size();
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return packageList.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder = null;
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.package_one_row, null);
                    viewHolder = new ViewHolder();
                    viewHolder.package_complany_name = (TextView) convertView.findViewById(R.id.package_detail_name);
                    viewHolder.package_complany_address = (TextView) convertView.findViewById(R.id.package_detail_address);
                    viewHolder.package_complany_phone = (TextView) convertView.findViewById(R.id.package_detail_phone);
                    viewHolder.package_complany_price = (TextView) convertView.findViewById(R.id.package_detail_price);
                    viewHolder.complany_name_button = (Button) convertView.findViewById(R.id.list_complany_name);
                    viewHolder.complany_name_button.setTag(Integer.valueOf(position));// 为每一个view设置一个Tag,每个框中的view共用一个tag
                    viewHolder.complany_address_button = (Button) convertView.findViewById(R.id.list_complany_address);
                    viewHolder.complany_address_button.setTag(Integer.valueOf(position));// 为每一个view设置一个Tag,每个框中的view共用一个tag
                    viewHolder.complany_phone_button = (Button) convertView.findViewById(R.id.list_complany_phone);
                    viewHolder.complany_phone_button.setTag(Integer.valueOf(position));// 为每一个view设置一个Tag,每个框中的view共用一个tag
                    viewHolder.complany_price_button = (Button) convertView.findViewById(R.id.list_complany_detail);
                    viewHolder.complany_price_button.setTag(Integer.valueOf(position));// 为每一个view设置一个Tag,每个框中的view共用一个tag
                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                PackageListItem item = packageList.get(position);
                //设置TextView的文字
                viewHolder.package_complany_name.setText(item.getStoreName());
                viewHolder.package_complany_address.setText(item.getStoreAddress());
                viewHolder.package_complany_phone.setText(item.storeAreaCode.equals("") ? item.storePhone
                        : item.storeAreaCode + "-" + item.storePhone);
                viewHolder.package_complany_price.setText(item.getServicePrice());
                
                //设置监听
                viewHolder.complany_name_button.setOnClickListener(mListerner);
                viewHolder.complany_address_button.setOnClickListener(mListerner);
                viewHolder.complany_phone_button.setOnClickListener(mListerner);
                viewHolder.complany_price_button.setOnClickListener(mListerner);
    
                return convertView;
            }
        }
    
        static class ViewHolder {
            private TextView package_complany_name;
            private TextView package_complany_address;
            private TextView package_complany_phone;
            private TextView package_complany_price;
            private Button complany_name_button;
            private Button complany_address_button;
            private Button complany_phone_button;
            private Button complany_price_button;
        }
    }
  • 相关阅读:
    用脚本实现对pm2.5 数据的获取
    虚拟机下的快盘空间分配方案
    C++中new的用法
    只在堆或栈上生成对象
    Virtualbox识别USB设备
    最长公共字序列
    gdb 打印vector 等stl数据
    LC_CTYPE: cannot change locale
    字符串压缩
    对象的概念
  • 原文地址:https://www.cnblogs.com/linxiaojiang/p/2962094.html
Copyright © 2011-2022 走看看