我正在使用片段,其中我有ListView
和全选复选框。
片段类..
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fb_custom_listview = (ListView) selectfriend_login
.findViewById(R.id.friend_listview);
select_all=(CheckBox) selectfriend_login.findViewById(R.id.select_all_checkbox);
select_all.setOnCheckedChangeListener(this);
}
现在custom adapter class extends Baseadapter
我有一个ImageView
,一个TextView
和一个checkbox
,layout
并在baseadapter类的getview方法中膨胀布局。一切都很好,它显示图像,显示文本并显示复选框。所以现在当我单击select_all(fragment class)
所有复选框时应选中自定义适配器类的复选框,未选中则所有未选中。这是自定义适配器的类
public class CustomListAdapter extends BaseAdapter
{
private Context contxt;
private ArrayList<String> user_friend_name;
private ArrayList<String> user_friend_pic_url;
private LayoutInflater inflater;
private int friend_counter=0;
private ChangeUIListner mlistner;
public static ArrayList<Integer> selected_friend_pos;
public static ArrayList<String> selected_friend_uid;
public static ArrayList<String> selected_friend_name;
private List<Model> list;
private ViewHolder viewholder=null;
public CustomListAdapter(Context context,ArrayList<String> user_friend_picurl, List<Model> model) {
// TODO Auto-generated constructor stub
this.contxt=context;
this.list=model;
user_friend_pic_url=user_friend_picurl;
selected_friend_uid=new ArrayList<String>();
selected_friend_name=new ArrayList<String>();
selected_friend_pos=new ArrayList<Integer>();
user_friend_name=GetFriendDetails.user_fb_friend_name;
inflater=(LayoutInflater) contxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
System.gc();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return user_friend_pic_url.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null)
{
viewholder=new ViewHolder();
convertView=inflater.inflate(R.layout.custom_listview_friendlist, null);
viewholder.fb_friend_name=(TextView) convertView.findViewById(R.id.friend_nametextview);
viewholder.fb_friend_pic=(ImageView) convertView.findViewById(R.id.friend_picview);
viewholder.fb_checkbox=(CheckBox) convertView.findViewById(R.id.friendselectcheckBox);
viewholder.fb_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int getposition=(Integer) buttonView.getTag();
Log.e("ITEM POSITION", "."+getposition);
list.get(getposition).setSelected(buttonView.isChecked());
if(isChecked==true)
{
if(selected_friend_pos.contains(getposition))
{
//nothing
}
else
{
friend_counter++;
mlistner.UpdateUi(friend_counter);
selected_friend_pos.add(getposition);
selected_friend_uid.add(GetFriendDetails.user_fb_friend_id.get(getposition));
selected_friend_name.add(GetFriendDetails.user_fb_friend_name.get(getposition));
Log.e("ARRAY SIZE", "."+selected_friend_uid.size());
}
}
else
{
if(selected_friend_pos.contains(getposition)){
friend_counter--;
mlistner.UpdateUi(friend_counter);
int del_posi=selected_friend_pos.indexOf(getposition);
selected_friend_pos.remove(del_posi);
selected_friend_name.remove(del_posi);
selected_friend_uid.remove(del_posi);
Log.e("ARRAYLIST", "REM"+del_posi);
Log.e("ARRAY SIZE", "."+selected_friend_uid.size());
}
}
}
});
convertView.setTag(viewholder);
}
else
{
viewholder=(ViewHolder) convertView.getTag();
new AsyncDownloadImage(viewholder).execute(user_friend_pic_url.get(position));
}
viewholder.fb_checkbox.setTag(position);
viewholder.fb_checkbox.setChecked(list.get(position).isSelected());
viewholder.fb_friend_name.setText(user_friend_name.get(position));
viewholder.fb_friend_name.setTag(user_friend_name.get(position));
viewholder.fb_friend_pic.setTag(user_friend_pic_url.get(position));
//new AsyncDownloadImage(viewholder).execute(user_friend_pic_url.get(position));
return convertView;
}
class ViewHolder
{
int id;
ImageView fb_friend_pic;
TextView fb_friend_name;
CheckBox fb_checkbox;
}
public class AsyncDownloadImage extends AsyncTask<String, Object, Bitmap>
{
private HttpURLConnection connection;
private InputStream is;
private Bitmap bitmap;
private URL url;
ViewHolder holder;
public AsyncDownloadImage(ViewHolder viewholder1) {
// TODO Auto-generated constructor stub
holder=viewholder1;
}
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
try {
url=new URL(params[0]);
try {
connection=(HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
is=connection.getInputStream();
bitmap=BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(is!=null)
{
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(connection!=null)
{
connection.disconnect();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
if(result!=null)
{
holder.fb_friend_pic.setImageBitmap(result);
}
}
}
public void registerUIupdateListener(ChangeUIListner lisnter)
{
mlistner=lisnter;
}
}
如您所见,我已经为列表视图复选框使用了单选功能,因此它工作正常并添加到数组列表中以进行进一步操作。所以当单击或选择复选框时,我们如何实现全选。谢谢