欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Android開發:setContentView切換界面,自定義

系統 2799 0

問題背景:

我在其他Activity里有一個數據庫,里面有若干條目,數據庫里存的是最簡單的“名字”string類型的信息。我在另外一個Activity里,通過按鍵Button,顯示出一個帶checkbox的列表,顯示出數據庫里的姓名,然后可以選中多個。類似于文件夾刪除的功能。

下面是實現:

第一部分,在布局文件夾下新建一個my_checkbox.xml.

這個布局是用來控制將來listview里,每一行怎么顯示。在這里我是左邊顯示名字,右邊顯示復選框CheckBox。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/item_text"
android:textSize="25dip"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/item_check"
android:textSize="25dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>

</LinearLayout>

注意:

1,上面的TextView里的layout_weight=1用來實現最左邊顯示名字,最右邊顯示CheckBox。

2,由于CheckBox的響應優先級高于ListView,這里需要把CheckBox的clickable和focuseable屬性都關閉。將來只通過listview的item是否點擊來判斷。

3,CheckBox的checkMark用來設置當選中之后,是個什么效果。

第二部分:新建一個布局list_check.xml,用來顯示數據庫的條目。當在主Activity里點擊后就會切換到這個界面。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

<Button

android:id="@+id/confirmBtn"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=" 確定 " />

<ListView

android:id="@+id/checkList"

android:layout_width="fill_parent"

android:layout_height="wrap_content" >

</ListView>

</LinearLayout>

注: 上面的button是選中若干條目后的確定按鍵,同時也是返回按鍵,返回到主界面。

第三部分 :在主Activity里設置利用setContentView的方法切換頁面。

這里主Activity的布局文件就不提供了。

下面是主Activity的代碼:

    //為了實現新的布局
	Button mChoseBtn = null;
	Button mConfirmBtn = null;

	boolean firstFlag = true;
	ListView list2 = null;
	View checkListView = null;
	View mainView = null;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LayoutInflater inflater = this.getLayoutInflater();
		checkListView = inflater.inflate(R.layout.list_check, null);
		mainView = inflater.inflate(R.layout.main, null);



		setContentView(mainView);
		//切換布局監聽
		mChoseBtn = (Button)mainView.findViewById(R.id.choseBtn);
		mChoseBtn.setOnClickListener(new ButtonListener());
		setUpViews();


	}

	class ButtonListener implements OnClickListener{

		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()){
			case R.id.choseBtn:
		
				Jump2CheckList();
				break;
			case R.id.confirmBtn:
				String s = getCheckInfo();
				showToast("您選中的姓名有:"+ s);
				Jump2Main();
				break;
			default:
				break;
			}
		}

	}

  


    	/*切換到主布局*/
	public void Jump2Main(){
		setContentView(mainView);		
		setUpViews();
	}

  


    	/*切換到選中布局*/
	public void Jump2CheckList(){
		setContentView(checkListView);
		if(firstFlag){
			mConfirmBtn = (Button)checkListView.findViewById(R.id.confirmBtn);
			mConfirmBtn.setOnClickListener(new ButtonListener());
			firstFlag = false;
		}
		initCheckList();


	}

  


第四部分:給ListView寫適配器 ,其實很簡單 也就是上面的initCheckList函數,包含初始化ListView和適配器兩個部分。先看源碼:

    	public void initCheckList(){
		list2 = (ListView)(checkListView).findViewById(R.id.checkList);
		list2.setItemsCanFocus(false);
		list2.setAdapter(new CheckListAdapter(this, cursor));
	    list2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	    list2.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View view, int positon,
					long id) {
				// TODO Auto-generated method stub
				ViewHolder vHolder = (ViewHolder) view.getTag();
				vHolder.check.toggle();
				isSelected.put(positon, vHolder.check.isChecked());
				
				
			}
		});
	}

  


下面是適配器:

    /*給CheckList設置適配器*/
	public static  Map<Integer, Boolean> isSelected;
	public  class CheckListAdapter extends BaseAdapter{

		private Context mContext;
		private Cursor mCursor;
		

		//構造函數
		public CheckListAdapter(Context context, Cursor cursor){
			mContext = context;
			mCursor = cursor;
			
			isSelected = new HashMap<Integer, Boolean>();
			for(int i=0; i<mCursor.getCount(); i++){
				isSelected.put(i, false);
			}
		}



		public int getCount() {
			// TODO Auto-generated method stub
			return cursor.getCount();
		}

		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if(convertView == null){
				holder = new ViewHolder();
				LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.my_checkbox, null);
				holder.text = (TextView) convertView.findViewById(R.id.item_text);
				holder.check = (CheckBox)convertView.findViewById(R.id.item_check);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder)convertView.getTag();
			}
		
			
			mCursor.moveToPosition(position);
			holder.text.setText(Integer.toString(mCursor.getInt(0)));
			holder.text.append(mCursor.getString(1));
			holder.check.setChecked(isSelected.get(position));
			return convertView;

		}
		
		public final class ViewHolder{
			public TextView text;
			public CheckBox check;
		}

	}

  


注:

1,initCheckList里要設置相應的參數,如多選等。

2, public static Map<Integer, Boolean> isSelected; 這是一個 全局變量 ,且是靜態的,用來存儲checkbox的選中狀態。 http://mobile.51cto.com/android-254823.htm 這里將其設成適配器里的一個靜態變量,但奇怪的是到我這就不中了,暫且弄成全局的吧。

3,在適配器的構造函數里初始化上面這個變量,并且所有都設成未選中。

isSelected = new HashMap<Integer, Boolean>();

for(int i=0; i<mCursor.getCount(); i++){

isSelected.put(i, false);

4,由于我跟數據庫做了關聯,所以在適配器的構造函數里傳進去一個cursor,關于cursor的理解可以參考 http://www.cnblogs.com/TerryBlog/archive/2010/07/05/1771459.html 說白了他就是一組信息,是個集合。通過cursor.getCount獲得他有多少行的信息。cursor.MoveToposition(i)定位到第i行。獲得數據的方法跟數據時建的表的結構有關。

5,

public final class ViewHolder{

public TextView text;

public CheckBox check;

}

至于這個viewholder,實際上不用也可以。他就是把每一行的元素集成了一下,跟布局相對應。但到后來莫名其妙的要用到View.setTag()和View.getTag()來傳遞數據,所以我又把他加上了。

6,適配器的兩大關鍵。

第一個是

public int getCount() {

// TODO Auto-generated method stub

return cursor.getCount();

}

這里是返回list有多少行。調用 ListView.getCount() 實際上就是在調用這個函數。

第二個就是 :public View getView(int position, View convertView, ViewGroup arg2)這個函數。注意第二個參數convertView就是給每一行設置的布局。通過

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = inflater.inflate(R.layout.my_checkbox, null);

將第一個布局文件,和view關聯起來。至于convertView.setTag(holder);其實是給view貼標簽,也就是傳數據的一種方式。當這個布局不為空時通過holder = (ViewHolder)convertView.getTag();直接獲得。mCursor包含所有行的信息,

mCursor.moveToPosition(position);這句話將其定位到position的位置,

holder.text.setText(Integer.toString(mCursor.getInt(0)));

holder.text.append(mCursor.getString(1));

這兩句話是獲得每行的信息。我這個表的結構是第一列(對應索引為0)是一個int,第二列(索引為1)是一個string。

holder.check.setChecked(isSelected.get(position));這句話是設置checkbox的狀態。 也就是說通過isSelected來設定,他的初始態是全不選。所以每次跳轉時,默認的是都不選。

最后程序 return convertView;返回這個view,然后當下次調用時,他又當做參數傳進來。

7,解釋下listview設置監聽:

list2.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View view, int positon,

long id) {

// TODO Auto-generated method stub

ViewHolder vHolder = (ViewHolder) view.getTag();

vHolder.check.toggle();

isSelected.put(positon, vHolder.check.isChecked());

}

});

當每個item被點擊時,通過被點擊的view.getTag獲得具體的控件view, CheckBox的狀態反轉。

isSelected.put(positon, vHolder.check.isChecked());這句話是通過訪問position位置的CheckBox的狀態來更新保存信息的isSelected。

8, 最后就是我怎么獲得選中的信息?

可以這么寫:

    
      OnClickListener bPop = new OnClickListener() {    
        @Override    
        public void onClick(View v) {    
            for(int i=0;i<list.getCount();i++){    
                if(MyAdapter.isSelected.get(i)){    
                    ViewHolder vHollder = (ViewHolder) list.getChildAt(i).getTag();    
Log.i(TAG, "--onClick --"+vHollder.title.getText());    
                }    
            }    
        }    
    }; 
    
  

通過list.getCount進行遍歷,通過list.getChildAt(i).getTag得到被選中的ViewHolder,然后得到信息。 因為這里我跟數據庫掛了鉤,所以我直接讀Cursor里面的信息就可以了。

我的寫法是:

    
      	public String getCheckInfo()
	{
		String info = "";
		for(int i=0; i<list2.getCount(); i++){
			if(isSelected.get(i)){
				//ViewHolder holder = (ViewHolder)list2.getChildAt(i).getTag();
				cursor.moveToPosition(i);
				info+=cursor.getInt(0)+".";
			}
		}
		return info;
	}

    
  

上面的list2.getCount和cursor.getCount是一樣的效果。

最后說下這個Cursor,因為他包含了數據庫里所有行的信息,所以我直接用他來填充到每個item。如果這個填充的信息是其他的,就是用到其他數據結構了,如 List<Map<String,Object>>mData; 來保存信息。具體可以參考: http://mobile.51cto.com/android-254823.htm

http://blog.csdn.net/a859522265/article/details/8204646 http://www.linuxidc.com/Linux/2011-11/47179p2.htm http://blog.sina.com.cn/s/blog_65570a20010108lp.html http://bbs.csdn.net/topics/330062289 我主要參考的第一篇。

另外,在數據庫里怎么獲得Cursor呢?

cursor = mPalmDB.select();

select()函數的封裝是:

    
      	public Cursor select(){		
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
		return cursor;
		
	}

    
  


這個可以封裝在數據庫類里,也可以寫到函數里。

源碼連同數據庫操作部分改日再提供哈!

效果圖:

1,主界面

Android開發:setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實現

2,在數據庫里添加兩個數據后:

Android開發:setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實現

3,點擊上面的“選定”按鍵,切換到另外一個界面

Android開發:setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實現

4,選中這兩個條目:

Android開發:setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實現

源碼下載: http://download.csdn.net/detail/yanzi1225627/5226894

歡迎android愛好者加群248217350 備注:yanzi

----------------------------本文系原創,轉載請注明作者:yanzi1225627

Android開發:setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實現


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 中文字幕日韩一区二区 | 国产不卡视频在线播放 | 暖暖日本在线播放 | 亚洲日本视频 | 亚州人成网在线播放 | 99这里只有精品视频 | 日韩在线视频在线 | 国产目拍亚洲精品区一区 | 亚洲日本高清成人aⅴ片 | 亚洲六月丁香色婷婷综合久久 | 色综合五月 | 成a人v在线观看视频 | 亚洲午夜大片 | 二区在线观看 | 色综合色综合网色综合 | 国产亚洲欧美一区 | 911网站大全在线观看 | 欧美在线成人影院 | 99久久免费视频在线观看 | 午夜电影通 | 欧美激情xxxx| 国产成人综合网 | 久久免费在线视频 | 免费一区 | 精品视频在线免费看 | xxxxhdvideosex| 国产精品一区二区三区免费 | 国产成人啪精品视频免费网站软件 | 天天干天天拍天天操 | 欧美一区在线观看视频 | 成人精品视频一区二区三区 | 日韩av免费 | 日日夜夜拍拍 | 5g免费影院永久天天影院在线 | 色一级| 国产97人妻人人做人碰人人爽 | 色综合久久中文色婷婷 | 91新视频 | 达达兔午夜起神影院在线观看麻烦 | 欧美视频一区二免费视频 | 草草网站 |