代码:
package com.xjkj.gl.regist;
import java.util.ArrayList;
import java.util.List;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
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.ListView;
import android.widget.TextView;
import com.fly.gx_sdk.string.UtilsSP;
import com.xjkj.gl.R;
import com.xjkj.gl.adapter.RegistListAdapter;
import com.xjkj.gl.adapter.RegistListAdapter.viewHolder;
import com.xjkj.gl.base.BaseActivityf;
import com.xjkj.gl.dataarray.DrawableArray;
import com.xjkj.gl.util_string.CharArray;
/**
* @author:moshangpiaoxue
* @file_name:RegistActivity4.java
* @todo:注册界面--选择最爱玩的游戏
* @date:2016-3-18
* @time: 下午5:05:36
*/
@ContentView(R.layout.registac3)
public class RegistAc4 extends BaseActivityf implements OnItemClickListener{
@ViewInject(R.id.bar_biao_left)private TextView bar_biao_left;
@ViewInject(R.id.bar_biao_right)private TextView bar_biao_right;
@ViewInject(R.id.regist3_tv)private TextView regist3_tv;
@ViewInject(R.id.lv_tag)private ListView lv_tag;
public List<Integer> logoList=new ArrayList<Integer>();
private DrawableArray array;
private CharArray charArray;
RegistListAdapter adapter;
private int checkNum=0; // 记录选中的条目数量
/** 存储点击选中的数据*/
private List<String> checkList=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
bar_biao_left.setText(R.string.regist);
regist3_tv.setText(R.string.love_play);
init();
}
private void init() {
for (int i = 0; i < array.logoList.length; i++) {
logoList.add(array.logoList[i]);
}
adapter=new RegistListAdapter( logoList,this);
// lv_tag.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//设置为多选
lv_tag.setAdapter(adapter);
lv_tag.setOnItemClickListener(this) ;
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
// TODO Auto-generated method stub
UtilsSP.put(this, "likegame", charArray.logoStr[position]);
viewHolder holder = (viewHolder) view.getTag();
holder.regist3_listitem_cb.toggle();// 在每次获取点击的item时改变checkbox的状态
checkNum = checkList.size();
adapter.isSelected.put(position, holder.regist3_listitem_cb.isChecked()); // 同时修改map的值保存状态
if (holder.regist3_listitem_cb.isChecked()) {
if (checkNum<3) {
holder.regist3_listitem_cb.setChecked(true);
checkList.add(position+1+"");
checkNum++;
} else {
holder.regist3_listitem_cb.setChecked(false);
checkList.remove(position+1+"");
checkNum--;
showToast("最多3个");
}
}else {
holder.regist3_listitem_cb.setChecked(false);
checkList.remove(position+1+"");
checkNum--;
}
}
@Event(value={R.id.bar_biao_right,R.id.bar_biao_left})
private void ckick(View v) {
switch (v.getId()) {
case R.id.bar_biao_right:
ArrayList arrayList=new ArrayList<>();
for (int i = 0; i < checkList.size(); i++) {
arrayList.add("\""+charArray.logoStr[(Integer.parseInt(checkList.get(i))-1)]+"\"");
}
Log.i("aaa", arrayList.toString());
UtilsSP.put(this, "likegame", arrayList.toString());
startActivity(new Intent(this,RegistAc5.class));
break;
case R.id.bar_biao_left:
finish();
break;
}
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/regist3_listitem_ll"
style="@style/w_match_h_match"
android:background="@color/bg_Blue_Light"
android:orientation="vertical" >
<ImageView
android:id="@+id/regist3_listitem_iv"
style="@style/w_match_h_match"
android:padding="10dp"
android:src="@drawable/logo_1" />
<CheckBox
android:id="@+id/regist3_listitem_cb"
style="@style/w_wrap_h_wrap"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:visibility="gone" />
</LinearLayout>
适配器;
package com.xjkj.gl.adapter;
import java.util.HashMap;
import java.util.List;
import org.xutils.x;
import org.xutils.view.annotation.ViewInject;
import com.xjkj.gl.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
/**
*
* @author:moshangpiaoxue
* @file_name:RegistListAdapter.java
* @todo:注册界面3-listview适配器
* @date:2016-3-22
* @time: 上午10:04:48
*/
public class RegistListAdapter extends BaseAdapter{
private Context context ;
private List<Integer> list;
public static HashMap<Integer, Boolean> isSelected;
public RegistListAdapter(List<Integer> list, Context context) {
super();
this.list = list;
this.context = context;
isSelected = new HashMap<Integer, Boolean>();
for (int i = 0; i < list.size(); i++) {
isSelected.put(i, false);
}
}
@Override
public int getCount() {
if (list==null) {
return 0;
}
return list.size();
}
@Override
public Object getItem(int arg0) {
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
viewHolder holder = null;
if (convertView == null) {
holder = new viewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.registac3_listitem, null);
x.view().inject(holder, convertView);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
holder.regist3_listitem_iv.setImageResource(list.get(position));
holder.regist3_listitem_cb.setChecked(isSelected.get(position));
return convertView;
}
public class viewHolder{
@ViewInject(R.id.regist3_listitem_iv)public ImageView regist3_listitem_iv;
@ViewInject(R.id.regist3_listitem_ll)public LinearLayout regist3_listitem_ll;
@ViewInject(R.id.regist3_listitem_cb)public CheckBox regist3_listitem_cb;
}
}