如何使用listview中的不可见项解析getchildat(index)为null

时间:2013-12-27 09:43:49

标签: android listview checkbox android-listview

我有一个带有复选框(checkall)和列表视图的主布局。我有一个baseadapter包括:图像和复选框。当我setadapter到listview时,我有10个项目。但listview显示7项和3项是不可见的。复选框(checkall)检查全部或取消选中listview中的所有复选框。检查checkall时,我得到了7个可见项的getchildat(index)。并且3个不可见的复选框getchildat(index)为null。如何getchildat()所有项目?。抱歉我的英语!

这是我的代码

BaseAdapter

public class EPGChannelSelectAdapter extends BaseAdapter{
    private List<EpgInfo> data;
    private AQuery aq;
    private ArrayList<EpgInfo> origData;

    public EPGChannelSelectAdapter(EpgInfo[] object) {
        data = new ArrayList<EpgInfo>(Arrays.asList(object));
    }

    public int getCount() {
        return data.size();
    }

    public void add(EpgInfo[] object) {
        data.addAll(Arrays.asList(object));
    }

    public EpgInfo getItem(int position) {
        return data.get(position);
    }

    public long getItemId(int position) {
        return position;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        aq = new AQuery(parent.getContext());
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.list_channel_epg_selected, parent,
                    false);

            holder = new ViewHolder();
            holder.tvName = (TextView) convertView
                    .findViewById(R.id.tv_video_title);
            holder.tvCountView = (TextView) convertView
                    .findViewById(R.id.tv_video_count_view);
            holder.ivThumbnail = (ImageView) convertView
                    .findViewById(R.id.iv_video_thumb);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.check_all);
            holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    if(isChecked)
                        buttonView.setChecked(true);
                    else
                        buttonView.setChecked(false);
                }
            });
            int width = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_SP, 75, parent.getContext()
                            .getResources().getDisplayMetrics());
            int height = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_SP, 45, parent.getContext()
                            .getResources().getDisplayMetrics());
            holder.ivThumbnail.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        EpgInfo cl = new EpgInfo();
        cl = data.get(position);

        aq.id(holder.ivThumbnail).progress(android.R.id.progress)
                .image(cl.getChannel().getThumbnail2(), true, true);
        holder.tvName.setText(cl.getChannel().getName());
        holder.tvCountView.setText(String.valueOf(cl.getChannel().getCountView()));
        return convertView;
    }

    private static class ViewHolder {
        TextView tvName;
        TextView tvCountView;
        ImageView ivThumbnail;
        CheckBox checkbox;

    }
    public void clearAll() {
        data.clear();
        notifyDataSetChanged();
    }

这是MainActivity:

public class EPGChannelSelectActivity extends FragmentActivity{

    private ListView list_channel_select;
    protected EPGChannelSelectAdapter mChannelAdapter;
    private CheckBox check_uncheck_all;
    private int[] arraychannelid;
    private String arrayIDChannel= "";

    final static String TAGEPGChannelSelectActivity = "epgchannelselect";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(Tag, "on onCreate()");
        setContentView(R.layout.select_epg);
         check_uncheck_all = (CheckBox) findViewById(R.id.check_all_static);
         list_channel_select = (ListView) findViewById(R.id.list_channel_select);


         if(userToken != null){
              userId = ((GlobalApp) getApplication()).getUserId();
              loadDataEPGbyUser(userId);
         }
         else
             loadDataEPG();
         back_epgselect.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                onBackPressed();
            }
        });
         /*
          * Check and uncheck all
          */
         check_uncheck_all.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int itemcount = list_channel_select.getCount();
                for(int i =0; i < itemcount;i++){
                    View view = list_channel_select.getChildAt(i);
                    if(view != null){
                    CheckBox checkbox = (CheckBox) view.findViewById(R.id.check_all);
                    checkbox.setChecked(true);
                    }
                    else
                        Log.d("EPGSelect", "null is at:" + i);
                }
            }
        });

    protected void loadDataEPGbyUser(int userId) {
        TimeTVApiCaller.getInstance().getEpgsByDate(date_receive, userId,
                true, getBaseContext(), new AjaxCallback<EpgInfo[]>(){
                    @Override
                    public void callback(String url, EpgInfo[] object,
                            AjaxStatus status) {
                        if (object != null && object.length > 0) {
                            mChannelAdapter = new EPGChannelSelectAdapter(object);
                            list_channel_select.setAdapter(mChannelAdapter);
                            mChannelAdapter.notifyDataSetChanged();
                            progressBar.setVisibility(View.GONE);
                        }
                    }
        });
    }
    protected void loadDataEPG(){
        TimeTVApiCaller.getInstance().getEpgsByDate2(date_receive,
                getBaseContext(), new AjaxCallback<EpgInfo[]>(){
            @Override
            public void callback(String url, EpgInfo[] object, AjaxStatus status) {
                if (object != null && object.length > 0) {
                    mChannelAdapter = new EPGChannelSelectAdapter(object);
                    list_channel_select.setAdapter(mChannelAdapter);
                    mChannelAdapter.notifyDataSetChanged();
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
    }


}

主要布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f1f1f1" >
    <RelativeLayout 
        android:id="@+id/select_all_epg"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_alignParentTop="true"
        android:background="#24242c">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CHỌN KÊNH"
            android:textSize="17sp"
            android:textColor="#e3e3e4"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="7dp"/>
        <CheckBox 
            android:id="@+id/check_all_static"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:button="@drawable/box_check_uncheck"
            />
    </RelativeLayout>
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/select_all_epg"
        android:layout_above="@+id/btn_selected">
    <ListView 
        android:id="@+id/list_channel_select"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" />
    <View 
        android:id="@+id/divider_epg_add"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#cfcbc1"
        android:layout_below="@+id/list_channel_select"
        android:layout_marginTop="0dp"
        android:visibility="invisible"/>
    <ProgressBar 
        android:id="@+id/progress_epg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>
    </RelativeLayout>
    <ImageView 
        android:id="@+id/btn_selected"
        android:layout_width="220dp"
        android:layout_height="65dp"
        android:src="@drawable/ic_btn_selected"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="60dp"/>
</RelativeLayout>

适配器布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/vtcplay.vtc.vn"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="7dp" >

    <ImageView
        android:id="@+id/iv_video_thumb"
        android:layout_width="50dp"
        android:layout_height="65dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:scaleType="fitXY"
         />

    <TextView
        android:id="@+id/tv_video_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/iv_video_hd"
        android:layout_alignTop="@+id/iv_video_thumb"
        android:layout_marginLeft="8dp"
        android:layout_toRightOf="@+id/iv_video_thumb"
        android:text="Kenh"
        android:singleLine="true"
        android:textSize="17sp" />


    <ImageView
        android:id="@+id/tv_video_count_view_label"
        android:layout_width="23dp"
        android:layout_height="16dp"
        android:layout_alignLeft="@+id/tv_video_title"
        android:layout_below="@+id/tv_video_title"
        android:background="@drawable/ic_wacthed"
        android:textColor="#6c6c6c"
        android:textSize="12sp"
  />

    <TextView
        android:id="@+id/tv_video_count_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tv_video_count_view_label"
        android:layout_marginLeft="3dp"
        android:layout_toRightOf="@+id/tv_video_count_view_label"
        android:text="TextView"
        android:textColor="#6c6c6c"
        android:textSize="12sp"/>
        <CheckBox 
            android:id="@+id/check_all"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="8dp"
            android:button="@drawable/box_check_uncheck"
            />

</RelativeLayout>

你能帮助我吗?

0 个答案:

没有答案