ListView中CheckBox刷新消失解决方案二

之前那篇博客的那个点击单个checkbox刷新还是会消失

这次开发中再次用到,优化了下,其实不难就是开发中经常接触这种组件组合

首先主Activity:

package com.listviewtest;

import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListViewTest extends ListActivity {
	
	/** Called when the activity is first created. */  
    Context context = null;   
    List<Integer> mList = new ArrayList<Integer>();
    CheckBox checkbox;
	private RelativeLayout mRelativeLayout;
    
    @Override  
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
        context = getApplicationContext();   
        setListAdapter(new MyListAdapter());   
    }   
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		mRelativeLayout = (RelativeLayout) v;
		checkbox = (CheckBox) mRelativeLayout.getChildAt(1);
		if (mList.contains(new Integer(position))) {
			checkbox.setChecked(false);
			mList.remove(new Integer(position));
		}else {
			checkbox.setChecked(true);
			mList.add(new Integer(position));
		}
	}
    
	class MyListAdapter extends BaseAdapter{   
        String data[] = new String[]{"11111", "22222", "33333", "44444","55555", "66666", "77777", "88888","99999", "10101", "12121", "13131"};   
        public int getCount() {   
            return data.length;   
        }   
  
        public Object getItem(int position) {   
            return data[position];   
        }   
  
        public long getItemId(int position) {   
            return position;   
        }   
  
        public View getView(int position, View convertView, ViewGroup parent) {   
            if (null == convertView){   
                LayoutInflater inflater      = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
                convertView = inflater.inflate(R.layout.list_item, parent, false);   
            }   
            TextView text       =   (TextView)convertView.findViewById(R.id.info);   
            checkbox   =   (CheckBox)convertView.findViewById(R.id.checkstatus);   
            checkbox.setTag(new Integer(position));   
            text.setText(data[position]);   
            if (mList != null) {
            	checkbox.setChecked((mList.contains(new Integer(position)))); 
			} else {
				checkbox.setChecked(false);   
			}
            return convertView;   
        }   
    }  
}


其次是main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
    <ListView       
        android:id ="@android:id/list"    
        android:layout_width="wrap_content"    
        android:layout_height="fill_parent"  
        android:scrollbarStyle="outsideOverlay"
        android:cacheColorHint="#00000000"
        android:fadingEdge="none"/>  
</LinearLayout>  


 

还有list_item.xml

 

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="horizontal">  
    <TextView  
        android:id="@+id/info" android:layout_width="wrap_content"  
        android:layout_height="fill_parent" android:layout_alignParentLeft="true"
        android:paddingLeft="5dip"/>  
        
    <CheckBox  
        android:id="@+id/checkstatus" android:layout_width="wrap_content"  
        android:layout_height="wrap_content" android:focusable="false" 
        android:clickable="false" android:paddingRight="10dip"
        android:layout_alignParentRight="true"/>  
</RelativeLayout>  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值