点击ListView上的按钮,按钮背景变化,同时按钮上的字也发生变化。
贴上源码:这个是Activity的源码:
/** * @author Seawin龙 */ package com.cn.gordon.exhibition.walk.activity; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import org.http.gordon.oa.command.NetCallBack; import org.http.gordon.oa.command.NetUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.cn.gordon.exhibition.walk.adapter.RankingAdapter; import com.cn.gordon.exhibition.walk.application.GlobalVaries; import com.cn.gordon.exhibition.walk.db.server.ExhibitorsService; import com.cn.gordon.exhibition.walk.entity.ExhibitorsBean; import com.cn.gordon.exhibition.walk.utils.Utils; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; /** * @author Seawin龙 * */ public class RankingActivity extends Activity { private ListView listView; private Button btnBack,btnQuery; private ProgressDialog pd; private RankingAdapter rAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.rankinglisttopten); init(); setAction(); } /** * @author Seawin龙 */ public void init() { btnBack = (Button) findViewById(R.id.btn_back); btnQuery = (Button) findViewById(R.id.btn_query); listView = (ListView)findViewById(R.id.myrankinglistView); getRankingListTopTen(); } private void displayListView(List<ExhibitorsBean> mydbrankinglist){ ArrayList<HashMap<String, Object>> list; list = this.getItem(mydbrankinglist); rAdapter = new RankingAdapter(list, getApplicationContext()); listView.setAdapter(rAdapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Log.i("displayListView", "listView "+arg2+"\tclicked"); } }); } private void getRankingListTopTen() { GlobalVaries global = (GlobalVaries) getApplication(); //弹出等待对话框 pd = ProgressDialog.show(this, null, "加载中,请稍后...", false, false); //设置参数 Map<String, String> postvalues = new HashMap<String, String>(); postvalues.put("rankingListType", "104"); postvalues.put("exhibitionHashCode", global.getExhibitionHashCode()); new NetUtils(new NetCallBack(){ @Override public void getNet(NetCallBack_State arg0, String arg1) { System.out.println(arg1); if(pd != null && pd.isShowing()){ pd.dismiss(); pd.cancel(); } switch (arg0) { case error: Toast.makeText(RankingActivity.this, "网络资源错误:"+arg1, Toast.LENGTH_SHORT).show(); break; case noNet: Toast.makeText(RankingActivity.this, "网络未开启", Toast.LENGTH_SHORT).show(); break; case wrong: Toast.makeText(RankingActivity.this, "网络数据错误"+arg1, Toast.LENGTH_SHORT).show(); break; case ok: List<ExhibitorsBean> mytemprankinglist = getRankingList(arg1); System.out.println(">>>>>>>>>>>>>>>arg1:"+arg1); List<ExhibitorsBean> mydbrankinglist = getBeanFromDB(mytemprankinglist); displayListView(mydbrankinglist); break; default: break; } } }).read(this, false, Utils.RANKINGTOPTEN_URL, null, null, false, postvalues, "GBK", true, true); } protected List<ExhibitorsBean> getBeanFromDB(List<ExhibitorsBean> mytemprankinglist) { List<ExhibitorsBean> returnList = null; if (mytemprankinglist!=null) { Collections.sort(mytemprankinglist,new ComparatorExhibitors()); System.out.println(">>>>>>>>>>>>>>>mytemprankinglist.size():"+mytemprankinglist.size()); returnList = new ArrayList<ExhibitorsBean>(); ExhibitorsService eService = new ExhibitorsService(getApplicationContext()); ExhibitorsBean eBean; for (int i = 0; i < mytemprankinglist.size(); i++) { eBean = new ExhibitorsBean(); eBean = eService.queryExhibitorByExhibitorHashCode(mytemprankinglist.get(i).getHashCode()); System.out.println(">>>>>>>>>>>>>>>HashCode:"+mytemprankinglist.get(i).getHashCode()); returnList.add(eBean); } } return returnList; } protected List<ExhibitorsBean> getRankingList(String arg1) { List<ExhibitorsBean> list = new ArrayList<ExhibitorsBean>(); ExhibitorsBean bean = null; try { JSONArray ja = new JSONArray(arg1); JSONObject jo = null; for (int i = 0; i < ja.length(); i++) { jo = ja.getJSONObject(i); bean = new ExhibitorsBean(); bean.setHashCode(jo.optString("key")); bean.setNativeId(jo.optInt("value")); list.add(bean); } } catch (JSONException e) { e.printStackTrace(); } return list; } public ArrayList<HashMap<String,Object>> getItem(List<ExhibitorsBean> mydbrankinglist){ ArrayList<HashMap<String, Object>> item = new ArrayList<HashMap<String, Object>>(); if (mydbrankinglist!=null) { System.out.println(">>>>getItem内部:mynetrankinglist not null"); for (int i = 0; i < mydbrankinglist.size(); i++) { HashMap<String, Object> map = new HashMap<String, Object>(); ExhibitorsBean eBean = new ExhibitorsBean(); eBean = mydbrankinglist.get(i); if (eBean!=null) { map.put("item", (i+1)+" 、"+eBean.getNativeName()); if (eBean.getChangeCardFlag()==0) { map.put("buttontext","换名片"); }else if (eBean.getChangeCardFlag()==1) { map.put("buttontext","已换名片"); } System.out.println(">>>>>eBean.getNativeName():"+eBean.getNativeName()); item.add(map); }else{ System.out.println("eBean is null"); } } } return item; } private void setAction() { btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); btnQuery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent in = new Intent(RankingActivity.this,CheckoutResultActivity.class); startActivity(in); } }); } public class ComparatorExhibitors implements Comparator<ExhibitorsBean>{ @Override public int compare(ExhibitorsBean arg0, ExhibitorsBean arg1) { return arg1.getNativeId() - arg0.getNativeId(); } } }
然后是Adapter的源码:
1 package com.cn.gordon.exhibition.walk.adapter; 2 3 import java.text.SimpleDateFormat; 4 import java.util.ArrayList; 5 import java.util.Date; 6 import java.util.HashMap; 7 import java.util.Map; 8 9 import org.http.gordon.oa.command.NetCallBack; 10 import org.http.gordon.oa.command.NetUtils; 11 12 import com.cn.gordon.exhibition.walk.activity.R; 13 import com.cn.gordon.exhibition.walk.application.GlobalVaries; 14 import com.cn.gordon.exhibition.walk.db.server.BusiCardHolderService; 15 import com.cn.gordon.exhibition.walk.db.server.ExhibitorsService; 16 import com.cn.gordon.exhibition.walk.entity.ExhibitorsBean; 17 import com.cn.gordon.exhibition.walk.entity.MyBusiCardHolderBean; 18 import com.cn.gordon.exhibition.walk.utils.Utils; 19 20 import android.annotation.SuppressLint; 21 import android.app.Activity; 22 import android.app.ProgressDialog; 23 import android.content.Context; 24 import android.util.Log; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.view.View.OnClickListener; 28 import android.view.ViewGroup; 29 import android.widget.BaseAdapter; 30 import android.widget.Button; 31 import android.widget.TextView; 32 import android.widget.Toast; 33 34 public class RankingAdapter extends BaseAdapter { 35 36 private ArrayList<HashMap<String, Object>> list; 37 private Activity context; 38 ViewHolder holder = null; 39 private LayoutInflater listContainer; 40 private ProgressDialog pd; 41 private GlobalVaries global; 42 private boolean successFlag = false; 43 44 public RankingAdapter(ArrayList<HashMap<String, Object>> list,Activity context){ 45 this.list = list; 46 this.context = context; 47 listContainer = LayoutInflater.from(context); 48 } 49 50 @Override 51 public int getCount() { 52 return list.size(); 53 } 54 55 @Override 56 public Object getItem(int position) { 57 return list.get(position); 58 } 59 60 @Override 61 public long getItemId(int position) { 62 return position; 63 } 64 65 @SuppressWarnings("deprecation") 66 @Override 67 public View getView(final int position, View convertView, ViewGroup parent) { 68 69 if (convertView==null) { 70 holder = new ViewHolder(); 71 convertView = listContainer.inflate(R.layout.rankinglistviewitem, null); 72 holder.btnChangeCard = (Button) convertView.findViewById(R.id.rankingchangecardid); 73 holder.txtCompanyName = (TextView) convertView.findViewById(R.id.rankinglistitemid); 74 ExhibitorsBean eBean = new ExhibitorsBean(); 75 eBean = (ExhibitorsBean) list.get(position).get("eBean"); 76 if (eBean!=null) { 77 holder.txtCompanyName.setText((position+1)+"、"+eBean.getNativeName()); 78 if (eBean.getChangeCardFlag()==0) { 79 holder.btnChangeCard.setText("换名片"); 80 // holder.btnChangeCard.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.changecardbtn_pressed_bg)); 81 82 }else if (eBean.getChangeCardFlag()==1) { 83 holder.btnChangeCard.setText("已换名片"); 84 // holder.btnChangeCard.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.btn_changecard_changed)); 85 } 86 convertView.setTag(holder);//绑定ViewHolder对象 87 88 } 89 }else{ 90 holder = (ViewHolder)convertView.getTag();//取出ViewHolder对象 91 } 92 holder.btnChangeCard.setOnClickListener(new BtnClickListener(position, context)); 93 // holder.btnChangeCard.setOnClickListener(new OnClickListener() { 94 // @Override 95 // public void onClick(View v) { 96 //// ExhibitorsBean eBean = (ExhibitorsBean) list.get(position).get("eBean"); 97 //// if (eBean!=null&&eBean.getChangeCardFlag()==0) { 98 //// Toast.makeText(context, eBean.getNativeName()+ "Btn Clicked", Toast.LENGTH_SHORT).show(); 99 //// boolean flag = sendChangeBusiCardResulttoServer(eBean); 100 //// if (flag) { 101 //// holder.btnChangeCard.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.btn_changecard_changed)); 102 //// Toast.makeText(context, "名片交换成功", Toast.LENGTH_SHORT).show(); 103 //// } 104 //// } 105 // if (v.getId()==holder.btnChangeCard.getId()) { 106 // 107 // holder.btnChangeCard.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.btn_changecard_changed)); 108 // } 109 // } 110 // }); 111 return convertView; 112 } 113 114 @SuppressLint("SimpleDateFormat") 115 protected boolean sendChangeBusiCardResulttoServer(final ExhibitorsBean eBean) { 116 // TODO Auto-generated method stub 117 global = (GlobalVaries) context.getApplication(); 118 successFlag = false; 119 //弹出等待对话框 120 pd = ProgressDialog.show(context, null, "加载中,请稍后...", false, false); 121 //设置参数 122 Map<String, String> postvalues = new HashMap<String, String>(); 123 postvalues.put("aMoiHashCode", global.getUserHashCode()); 124 postvalues.put("zMoiHashCode", eBean.getHashCode()); 125 new NetUtils(new NetCallBack(){ 126 127 @Override 128 public void getNet(NetCallBack_State arg0, String arg1) { 129 System.out.println(arg1); 130 if(pd != null && pd.isShowing()){ 131 pd.dismiss(); 132 pd.cancel(); 133 } 134 switch (arg0) { 135 case error: 136 Toast.makeText(context, "网络资源错误:"+arg1, Toast.LENGTH_SHORT).show(); 137 break; 138 case noNet: 139 Toast.makeText(context, "网络未开启", Toast.LENGTH_SHORT).show(); 140 break; 141 case wrong: 142 Toast.makeText(context, "网络数据错误"+arg1, Toast.LENGTH_SHORT).show(); 143 break; 144 case ok: 145 //将数据库中的展商交换过名片设置为已交换 146 ExhibitorsService eService = new ExhibitorsService(context); 147 eService.setExhibtorChangeCard(eBean.getHashCode()); 148 //向名片夹数据库中存储一条名片记录 149 BusiCardHolderService cardHolderService = new BusiCardHolderService(context); 150 MyBusiCardHolderBean cardBean = new MyBusiCardHolderBean(); 151 cardBean.setAddress(eBean.getAddress()); 152 cardBean.setCompanyLogoUrl(eBean.getImgUrl()); 153 cardBean.setCompanyName(eBean.getNativeName()); 154 cardBean.setCompanyPhonenum(eBean.getPhoneNumber()); 155 cardBean.setDelFlag(0); 156 cardBean.setExhibitionName(global.getExhibitionName()); 157 cardBean.setExhibitorHashCode(eBean.getHashCode()); 158 cardBean.setFax(eBean.getFaxNumber()); 159 SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 160 cardBean.setGetcardTime(sdf.format(new Date())); 161 cardBean.setTempEmail(eBean.getTempEmail()); 162 cardHolderService.addABusiCard(cardBean); 163 //改变当前页当前按钮的背景颜色 164 successFlag = true; 165 System.out.println("名片交换成功"); 166 break; 167 default: 168 break; 169 } 170 } 171 }).read(context, false, Utils.CHANGEBUSINESSCARD_URL, null, null, false, postvalues, "GBK", true, true); 172 return successFlag; 173 } 174 175 @Override 176 public void notifyDataSetChanged() { 177 // TODO Auto-generated method stub 178 super.notifyDataSetChanged(); 179 } 180 181 182 } 183 class BtnClickListener implements OnClickListener{ 184 private int pos; 185 private Activity context; 186 public BtnClickListener(int pos,Activity context) { 187 this.pos = pos; 188 this.context = context; 189 } 190 @SuppressWarnings("deprecation") 191 @Override 192 public void onClick(View v) { 193 v.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.btn_changecard_changed)); 194 } 195 196 } 197 class ViewHolder{ 198 public TextView txtCompanyName; 199 public Button btnChangeCard; 200 }
然后是listviewitem的布局文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" 6 android:descendantFocusability= "blocksDescendants" 7 android:background="@drawable/listview_pressed_bg" 8 > 9 10 <TextView 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_centerVertical="true" 14 android:id="@+id/rankinglistitemid" 15 android:textColor="@color/black" 16 android:textSize="18sp" 17 android:layout_marginLeft="15dip" 18 android:layout_marginTop="10dip" 19 android:layout_marginBottom="10dip" 20 /> 21 <Button 22 android:layout_width="60dip" 23 android:layout_height="30dip" 24 android:gravity="center" 25 android:layout_gravity="center" 26 android:layout_centerVertical="true" 27 android:id="@+id/rankingchangecardid" 28 android:text="@string/exchangecard" 29 android:textColor="@color/white" 30 android:textSize="14sp" 31 android:background="@drawable/changecardbtn_pressed_bg" 32 android:focusable = "false" 33 android:layout_alignParentRight="true" 34 android:layout_marginRight="15dip" 35 android:layout_marginTop="10dip" 36 android:layout_marginBottom="10dip"> 37 38 </Button> 39 </RelativeLayout>
这个布局文件中,
<RelativeLayout>下的
android:descendantFocusability= "blocksDescendants"
和
<Button>下的
android:focusable = "false"可以分离listview和Button的点击事件。