日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Android開發(fā):setContentView切換界面,自定義

系統(tǒng) 3162 0

問(wèn)題背景:

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

下面是實(shí)現(xiàn):

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

這個(gè)布局是用來(lái)控制將來(lái)listview里,每一行怎么顯示。在這里我是左邊顯示名字,右邊顯示復(fù)選框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用來(lái)實(shí)現(xiàn)最左邊顯示名字,最右邊顯示CheckBox。

2,由于CheckBox的響應(yīng)優(yōu)先級(jí)高于ListView,這里需要把CheckBox的clickable和focuseable屬性都關(guān)閉。將來(lái)只通過(guò)listview的item是否點(diǎn)擊來(lái)判斷。

3,CheckBox的checkMark用來(lái)設(shè)置當(dāng)選中之后,是個(gè)什么效果。

第二部分:新建一個(gè)布局list_check.xml,用來(lái)顯示數(shù)據(jù)庫(kù)的條目。當(dāng)在主Activity里點(diǎn)擊后就會(huì)切換到這個(gè)界面。

<?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是選中若干條目后的確定按鍵,同時(shí)也是返回按鍵,返回到主界面。

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

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

下面是主Activity的代碼:

    //為了實(shí)現(xiàn)新的布局
	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);
		//切換布局監(jiān)聽
		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寫適配器 ,其實(shí)很簡(jiǎn)單 也就是上面的initCheckList函數(shù),包含初始化ListView和適配器兩個(gè)部分。先看源碼:

    	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設(shè)置適配器*/
	public static  Map<Integer, Boolean> isSelected;
	public  class CheckListAdapter extends BaseAdapter{

		private Context mContext;
		private Cursor mCursor;
		

		//構(gòu)造函數(shù)
		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里要設(shè)置相應(yīng)的參數(shù),如多選等。

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

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

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

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

isSelected.put(i, false);

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

5,

public final class ViewHolder{

public TextView text;

public CheckBox check;

}

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

6,適配器的兩大關(guān)鍵。

第一個(gè)是

public int getCount() {

// TODO Auto-generated method stub

return cursor.getCount();

}

這里是返回list有多少行。調(diào)用 ListView.getCount() 實(shí)際上就是在調(diào)用這個(gè)函數(shù)。

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

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

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

將第一個(gè)布局文件,和view關(guān)聯(lián)起來(lái)。至于convertView.setTag(holder);其實(shí)是給view貼標(biāo)簽,也就是傳數(shù)據(jù)的一種方式。當(dāng)這個(gè)布局不為空時(shí)通過(guò)holder = (ViewHolder)convertView.getTag();直接獲得。mCursor包含所有行的信息,

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

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

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

這兩句話是獲得每行的信息。我這個(gè)表的結(jié)構(gòu)是第一列(對(duì)應(yīng)索引為0)是一個(gè)int,第二列(索引為1)是一個(gè)string。

holder.check.setChecked(isSelected.get(position));這句話是設(shè)置checkbox的狀態(tài)。 也就是說(shuō)通過(guò)isSelected來(lái)設(shè)定,他的初始態(tài)是全不選。所以每次跳轉(zhuǎn)時(shí),默認(rèn)的是都不選。

最后程序 return convertView;返回這個(gè)view,然后當(dāng)下次調(diào)用時(shí),他又當(dāng)做參數(shù)傳進(jìn)來(lái)。

7,解釋下listview設(shè)置監(jiān)聽:

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());

}

});

當(dāng)每個(gè)item被點(diǎn)擊時(shí),通過(guò)被點(diǎn)擊的view.getTag獲得具體的控件view, CheckBox的狀態(tài)反轉(zhuǎn)。

isSelected.put(positon, vHolder.check.isChecked());這句話是通過(guò)訪問(wèn)position位置的CheckBox的狀態(tài)來(lái)更新保存信息的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());    
                }    
            }    
        }    
    }; 
    
  

通過(guò)list.getCount進(jìn)行遍歷,通過(guò)list.getChildAt(i).getTag得到被選中的ViewHolder,然后得到信息。 因?yàn)檫@里我跟數(shù)據(jù)庫(kù)掛了鉤,所以我直接讀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是一樣的效果。

最后說(shuō)下這個(gè)Cursor,因?yàn)樗藬?shù)據(jù)庫(kù)里所有行的信息,所以我直接用他來(lái)填充到每個(gè)item。如果這個(gè)填充的信息是其他的,就是用到其他數(shù)據(jù)結(jié)構(gòu)了,如 List<Map<String,Object>>mData; 來(lái)保存信息。具體可以參考: 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 我主要參考的第一篇。

另外,在數(shù)據(jù)庫(kù)里怎么獲得Cursor呢?

cursor = mPalmDB.select();

select()函數(shù)的封裝是:

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

    
  


這個(gè)可以封裝在數(shù)據(jù)庫(kù)類里,也可以寫到函數(shù)里。

源碼連同數(shù)據(jù)庫(kù)操作部分改日再提供哈!

效果圖:

1,主界面

Android開發(fā):setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實(shí)現(xiàn)

2,在數(shù)據(jù)庫(kù)里添加兩個(gè)數(shù)據(jù)后:

Android開發(fā):setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實(shí)現(xiàn)

3,點(diǎn)擊上面的“選定”按鍵,切換到另外一個(gè)界面

Android開發(fā):setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實(shí)現(xiàn)

4,選中這兩個(gè)條目:

Android開發(fā):setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實(shí)現(xiàn)

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

歡迎android愛(ài)好者加群248217350 備注:yanzi

----------------------------本文系原創(chuàng),轉(zhuǎn)載請(qǐng)注明作者:yanzi1225627

Android開發(fā):setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實(shí)現(xiàn)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 年辖:市辖区| 桐庐县| 建昌县| 砀山县| 广昌县| 禄劝| 加查县| 沾益县| 沙田区| 邮箱| 宣城市| 广东省| 彭阳县| 万载县| 息烽县| 乌拉特后旗| 朝阳县| 土默特右旗| 安龙县| 云南省| 阿合奇县| 新巴尔虎左旗| 金阳县| 庆元县| 石家庄市| 焦作市| 时尚| 陕西省| 新疆| 宜都市| 西乌珠穆沁旗| 孙吴县| 隆林| 荣成市| 乌拉特中旗| 青田县| 侯马市| 洱源县| 库车县| 璧山县| 汝城县|