您的位置:首页 > 其它

ListView给item其中控件添加监听,在Adapter中添加,不难,你可以的。Context帮大忙了

2016-01-31 16:06 525 查看
public class ComputerBarAdapter extends BaseAdapter {

private List<Map<String, Object>> list;

private ViewHolder holder;

private Context context;

private Map<String, Object> map;

public ComputerBarAdapter() {

super();

}

public ComputerBarAdapter(List<Map<String, Object>> list, Context context) {

super();

this.list = list;

this.context = context;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return list.size();

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return list.get(position);

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

}

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

if (convertView == null) {

holder = new ViewHolder();

convertView = View.inflate(context, R.layout.item_computerbar, null);

holder.tvComputerBarName = (TextView) convertView.findViewById(R.id.tv_computerBar);

holder.tvTask5 = (TextView) convertView.findViewById(R.id.tv_task5);

holder.tvTask15 = (TextView) convertView.findViewById(R.id.tv_task15);

holder.tvConsumeNum = (TextView) convertView.findViewById(R.id.tv_consumeTimes);

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();

}

map = list.get(position);

//map=(Map<String, Object>) getItem(position);

//Log.i("TAG", "不同position的map"+map.toString());

holder.tvComputerBarName.setText(map.get("storeName").toString());

holder.tvTask5.setText(map.get("fiveStatusName").toString());

holder.tvTask15.setText(map.get("fifStatusName").toString());

holder.tvConsumeNum.setText(map.get("quantity").toString());

set5TextColer(map.get("fiveStatus").toString());

set15TextColer(map.get("fifStatus").toString());

/**t添加监听*/

holder.tvTask5.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

//UIHelper.showToast(context, "5yuanrenwu");

map=list.get(position);

set5Action();

Log.i("TAG", "从这里走");

}

});

holder.tvTask15.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

map=list.get(position);

set15Action();

//Log.i("TAG", "15元任务ID"+map.get("fifTaskId").toString());

}

});


return convertView;

}

class ViewHolder {

TextView tvComputerBarName;

TextView tvTask5;

TextView tvTask15;

TextView tvConsumeNum;

}

/**

* 设置5元任务文字颜色

*/

private void set5TextColer(String status) {

// status任务状态::0审核中,1审核未通过、未完成,2审核通过、未领取,3已领取

switch (status) {

case "0":

holder.tvTask5.setTextColor(Color.GRAY);

break;

case "1":

holder.tvTask5.setTextColor(Color.MAGENTA);

break;

case "2":

holder.tvTask5.setTextColor(Color.RED);

break;

case "3":

holder.tvTask5.setTextColor(Color.YELLOW);

break;

case "-1":

holder.tvTask5.setTextColor(Color.BLUE);

break;

default:

break;

}

}

/**

* 设置15元任务文字颜色

*/

private void set15TextColer(String status) {

// status任务状态::0审核中,1审核未通过、未完成,2审核通过、未领取,3已领取

switch (status) {

case "0":

holder.tvTask15.setTextColor(Color.GRAY);

break;

case "1":

holder.tvTask15.setTextColor(Color.MAGENTA);

break;

case "2":

holder.tvTask15.setTextColor(Color.RED);

break;

case "3":

holder.tvTask15.setTextColor(Color.YELLOW);

break;

case "-1":

holder.tvTask15.setTextColor(Color.BLUE);

break;

default:

break;

}

}

/**

* 5元点击事件

*/

private void set5Action() {

// TODO Auto-generated method stub

// TODO Auto-generated method stub

//TaskId是任务Id:1代表五元任务,2代表十五元任务

//status任务状态::0审核中,1审核未通过、未完成,2审核通过、未领取,3已领取

Log.i("TAG", "mapID"+map.get("fiveTaskId").toString());

switch (map.get("fiveStatus").toString()) {

case "0":

ShowDialog("任务审核中,请耐心等待",null);

break;

case "1":

ShowDialog(map.get("fiveStatusName").toString(),map.get("fiveTaskId").toString());

break;

case "2":

MyApplication.mRequestManager.getReceiveReward(map.get("fiveTaskId").toString(), map.get("storeId").toString(), (ResponseCallBack) context);

//ShowDialog("恭喜您领取成功",null);////////////////////////////这里需要发送领取请求

break;

case "-1":

getTaskType(map.get("fiveTaskId").toString());

break;

default:

break;

}

}

/**

* 15元点击

*/

private void set15Action() {

// TODO Auto-generated method stub

// TODO Auto-generated method stub

//TaskId是任务Id:1代表五元任务,2代表十五元任务

//status任务状态::0审核中,1审核未通过、未完成,2审核通过、未领取,3已领取,-1去完成

Log.i("TAG", "15mapID"+map.get("fifTaskId").toString());

Log.i("TAG", "15map状态编号"+map.get("fifStatus").toString());

Log.i("TAG", "15map状态名字"+map.get("fifStatusName").toString());

switch (map.get("fifStatus").toString()) {

case "0":

ShowDialog("任务审核中,请耐心等待",null);

break;

case "1":

ShowDialog(map.get("fifStatusName").toString(),map.get("fifTaskId").toString());

break;

case "2":

MyApplication.mRequestManager.getReceiveReward(map.get("fifTaskId").toString(), map.get("storeId").toString(), (ResponseCallBack) context);

//ShowDialog("恭喜您领取成功",null);////////////////////////////这里需要发送领取请求

break;

case "-1":

String quantity = map.get("quantity").toString();

int consumNum=Integer.parseInt(quantity);

if (consumNum>=5) {

getTaskType(map.get("fifTaskId").toString());

}else {

UIHelper.showToast(context, "网吧至少消费5个实名宝币才可以去完成");

}

//Log.i("TAG", "15元任务ID"+map.get("fifTaskId").toString());

break;

default:

break;

}

}

/**

* 弹窗

*/

private void ShowDialog(String message, final String taskId) {

// TODO Auto-generated method stub

AlertDialog.Builder builder = new Builder(context);

builder.setMessage(message);

builder.setTitle(MyApplication.mResources.getString(R.string.prompt));

builder.setPositiveButton(MyApplication.mResources.getString(R.string.confirm), new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

if (taskId != null) {

getTaskType(taskId);

}

}

});

builder.setCancelable(false);

builder.create().show();

}

/**

* 跳转

*/

private void getTaskType(String taskId) {

// TODO Auto-generated method stub

TaskBean taskBean = new TaskBean();

taskBean.setAccountId(map.get("accountId").toString());

taskBean.setStoreId(map.get("storeId").toString());

//taskBean.setTaskId(map.get(taskId).toString());

taskBean.setStoreCode(map.get("cybercafeCode").toString());

Intent intent = new Intent();

if (taskId.equals("1")) {

taskBean.setStoreName(map.get("storeName").toString());

Log.i("TAG", "5元任务map就是"+map.get("storeName").toString());

intent.putExtra("TaskBean",taskBean);

intent.setClass(context, Task5Activity.class);

}

else if(taskId.equals("2")) {

taskBean.setQuantity(map.get("quantity").toString());

Log.i("TAG", "15元任务map就是"+map.get("storeName").toString());

intent.putExtra("TaskBean",taskBean);

intent.setClass(context, Task15Activity.class);

}

context.startActivity(intent);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: